Ejemplo n.º 1
0
def load_batch(split_name, dataset, batch_size, height, width):
    data_provider = slim.dataset_data_provider.DatasetDataProvider(
        dataset,
        common_queue_capacity=24 + 3 * batch_size,
        common_queue_min=24)
    if split_name == "test":
        raw_image, img_name = data_provider.get(["image", "img_name"])
        image = preprocess_image(raw_image, height, width, False)
        #获取一个batch的数据
        images, img_names = tf.train.batch([image, img_name],
                                           batch_size=batch_size,
                                           num_threads=4,
                                           capacity=4 * batch_size,
                                           allow_smaller_final_batch=True)
        return images, img_names
    else:
        raw_image, img_label = data_provider.get(["image", "label"])
        #Perform the correct preprocessing for this image depending if it is training or evaluating
        image = preprocess_image(raw_image, height, width, True)
        #As for the raw images, we just do a simple reshape to batch it up
        raw_image = tf.expand_dims(raw_image, 0)
        raw_image = tf.image.resize_nearest_neighbor(raw_image,
                                                     [height, width])
        raw_image = tf.squeeze(raw_image)
        #获取一个batch数据
        images, raw_image, labels = tf.train.batch(
            [image, raw_image, img_label],
            batch_size=batch_size,
            num_threads=4,
            capacity=4 * batch_size,
            allow_smaller_final_batch=True)
        return images, raw_image, labels
Ejemplo n.º 2
0
def load_image(image_path, label, is_training):
    image_buffer = tf.read_file(image_path)
    image = tf.image.decode_png(image_buffer, channels=3)
    image = tf.cast(image, tf.float32)
    image = tf.cond(is_training,
                    true_fn=lambda: inception_preprocessing.preprocess_image(
                        image, 224, 224, is_training=True),
                    false_fn=lambda: inception_preprocessing.preprocess_image(
                        image, 224, 224, is_training=False))
    return image, label
Ejemplo n.º 3
0
def read_one_image(fname, **kwargs):
    """Reads one image given a filepath

    Parameters
    -----------
    fname : str
        path to a JPEG file
    img_shape : tuple
        (kwarg) shape of the eventual image. Default is (224, 224, 3)
    is_training : bool
        (kwarg) boolean to tell the loader function if the graph is in training
        mode or testing. Default is True

    Returns
    -------
    preprocessed image
    """
    img_shape = kwargs.pop("image_shape", (224, 224, 3))
    is_training = kwargs.pop("is_training", False)
    # read the image file
    content = tf.read_file(fname)

    # decode buffer as jpeg
    img_raw = tf.image.decode_jpeg(content, channels=img_shape[-1])

    return preprocess_image(img_raw, img_shape[0], img_shape[1], is_training=is_training)
Ejemplo n.º 4
0
def load_data_v2(image_paths,
                 do_random_crop,
                 do_random_flip,
                 image_size,
                 do_prewhiten=True,
                 sess=None):
    nrof_samples = len(image_paths)
    images = np.zeros((nrof_samples, image_size, image_size, 3))
    import cv2
    for i in range(nrof_samples):
        #img = misc.imread(image_paths[i])
        file_contents = tf.read_file(image_paths[i])
        img = tf.image.decode_jpeg(file_contents)
        img.set_shape((image_size, image_size, 3))
        # if img.ndim == 2:
        #     img = to_rgb(img)
        # if do_prewhiten:
        #     img = prewhiten(img)
        # img = crop(img, do_random_crop, image_size)
        # img = flip(img, do_random_flip)
        img = porcessor.preprocess_image(img,
                                         tf.constant(160, tf.int32),
                                         tf.constant(160, tf.int32),
                                         is_training=True)
        # img = tf.image.central_crop(img, central_fraction=0.875)
        img = sess.run(img)
        cv2.imwrite(image_paths[i] + "_" + str(i) + ".jpg", img)
        images[i, :, :, :] = img
    return images
Ejemplo n.º 5
0
    def init_cnn_graph(self, checkpoints_path):
        with self.graph.as_default():
            tf_global_step = tf.train.get_or_create_global_step()
            self.image_input = tf.placeholder(tf.float32,
                                              shape=(None, None, 3))
            image = inception_preprocessing.preprocess_image(
                self.image_input,
                self.inception_dim,
                self.inception_dim,
                is_training=False,
                center_crop=self.center_crop,
            )
            images = tf.expand_dims(image, 0)

            with slim.arg_scope(self.arg_scope):
                slim_args = [slim.batch_norm, slim.dropout]
                with slim.arg_scope(slim_args, is_training=False):
                    with tf.variable_scope('InceptionV3', reuse=None) as scope:
                        net, _ = inception.inception_v3_base(
                            images, final_endpoint=self.endpoint, scope=scope)
            self.net = tf.reduce_mean(net, [0, 1, 2])

            variable_averages = tf.train.ExponentialMovingAverage(
                self.moving_average_decay, tf_global_step)
            variables_to_restore = variable_averages.variables_to_restore()
            self.init_fn = slim.assign_from_checkpoint_fn(
                checkpoints_path, variables_to_restore)
def preproc(input_images, height=input_size, width=input_size):
    '''
    preprocess the input image for evaluation using inception-resnet-v2

    '''
    images = []
    raw_images = []
    for i in range(50):
        raw_image = input_images[i]
        #Perform the correct preprocessing for this image depending if it is training or evaluating
        image = inception_preprocessing.preprocess_image(raw_image,
                                                         height,
                                                         width,
                                                         is_training=False)
        image = tf.expand_dims(image, 0)

        #As for the raw images, we just do a simple reshape to batch it up
        raw_image = tf.expand_dims(raw_image, 0)
        raw_image = tf.image.resize_nearest_neighbor(raw_image,
                                                     [height, width])

        if i == 0:
            images = image
            raw_images = raw_image
        else:
            images = tf.concat([images, image], 0)
            raw_images = tf.concat([raw_images, raw_image], 0)

    return images, raw_images
Ejemplo n.º 7
0
def read_one_image(fname, **kwargs):
    """Reads one image given a filepath

    Parameters
    -----------
    fname : str
        path to a JPEG file
    img_shape : tuple
        (kwarg) shape of the eventual image. Default is (224, 224, 3)
    is_training : bool
        (kwarg) boolean to tell the loader function if the graph is in training
        mode or testing. Default is True

    Returns
    -------
    preprocessed image
    """
    img_shape = kwargs.pop("image_shape", (224, 224, 3))
    is_training = kwargs.pop("is_training", False)
    # read the image file
    content = tf.read_file(fname)

    # decode buffer as jpeg
    img_raw = tf.image.decode_jpeg(content, channels=img_shape[-1])

    return preprocess_image(img_raw,
                            img_shape[0],
                            img_shape[1],
                            is_training=is_training)
Ejemplo n.º 8
0
def load_batch(dataset,
               batch_size,
               num_classes,
               height,
               width,
               is_training=True):

    data_provider = slim.dataset_data_provider.DatasetDataProvider(
        dataset,
        common_queue_capacity=24 + 3 * batch_size,
        common_queue_min=24)
    xx = ["image"]
    for i in range(num_classes):
        xx.append("label_" + str(i))
    raw_image, label0, label1, label2, label3, label4, label5, label6, label7, label8, label9, label10, label11 = data_provider.get(
        xx)
    label = tf.stack([
        label0, label1, label2, label3, label4, label5, label6, label7, label8,
        label9, label10, label11
    ])
    image = inception_preprocessing.preprocess_image(raw_image, height, width,
                                                     is_training)

    raw_image = tf.expand_dims(raw_image, 0)
    raw_image = tf.image.resize_nearest_neighbor(raw_image, [height, width])
    raw_image = tf.squeeze(raw_image)

    images, raw_images, labels = tf.train.batch([image, raw_image, label],
                                                batch_size=batch_size,
                                                num_threads=4,
                                                capacity=4 * batch_size,
                                                allow_smaller_final_batch=True)

    return images, raw_images, labels
def load_batch(dataset, batch_size, height=0, width=0, is_training=True):

    data_provider = slim.dataset_data_provider.DatasetDataProvider(
        dataset,
        common_queue_capacity=24 + 3 * batch_size,
        common_queue_min=24)

    raw_image, label = data_provider.get(['image', 'label'])

    image = inception_preprocessing.preprocess_image(raw_image, height, width,
                                                     is_training)
    #image = tf.expand_dims(raw_image, 0)
    #image = tf.image.resize_nearest_neighbor(image, [height, width])
    #image = tf.squeeze(image)
    #image = tf.cast(image, tf.float32)

    raw_image = tf.expand_dims(raw_image, 0)
    raw_image = tf.image.resize_nearest_neighbor(raw_image,
                                                 [3 * height, 3 * width])
    raw_image = tf.squeeze(raw_image)

    images, raw_images, labels = tf.train.batch([image, raw_image, label],
                                                batch_size=batch_size,
                                                num_threads=4,
                                                capacity=4 * batch_size,
                                                allow_smaller_final_batch=True)

    return images, raw_images, labels
Ejemplo n.º 10
0
def preprocess(filename_queue, image_size):
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    image = tf.image.decode_image(value, channels=3)
    precessed_image = inception_preprocessing.preprocess_image(
        image, image_size, image_size, is_training=False)
    return key, precessed_image
Ejemplo n.º 11
0
 def preprocess_fn(path):
     image = tf.read_file(path)
     image = tf.image.decode_jpeg(image, channels=3)
     image = inception_preprocessing.preprocess_image(image,
                                                      FLAGS.width,
                                                      FLAGS.height,
                                                      is_training=False)
     return image
Ejemplo n.º 12
0
def load_batch(dataset,
               batch_size,
               height=image_size,
               width=image_size,
               is_training=True):
    '''
    Loads a batch for training.

    INPUTS:
    - dataset(Dataset): a Dataset class object that is created from the get_split function
    - batch_size(int): determines how big of a batch to train
    - height(int): the height of the image to resize to during preprocessing
    - width(int): the width of the image to resize to during preprocessing
    - is_training(bool): to determine whether to perform a training or evaluation preprocessing

    OUTPUTS:
    - images(Tensor): a Tensor of the shape (batch_size, height, width, channels) that contain one batch of images
    - labels(Tensor): the batch's labels with the shape (batch_size,) (requires one_hot_encoding).

    '''
    #First create the data_provider object

    data_provider = slim.dataset_data_provider.DatasetDataProvider(
        dataset,
        common_queue_capacity=24 + 3 * batch_size,
        common_queue_min=24)

    #BY using data_provider TO GET raw image
    raw_image, label = data_provider.get(['image', 'label'])

    #Perform the correct preprocessing for this image depending if it is training or evaluating
    image = inception_preprocessing.preprocess_image(raw_image, height, width,
                                                     is_training)

    #As for the raw images, we just do a simple reshape to batch it up
    ## keep raw image that is not preprocessed for the inception model so that
    #we can display it as an image in its original form. We only do a
    #simple reshaping so that it fits together nicely in one batch.
    #tf.expand_dims will expand the 3D tensor
    #from a [height, width, channels] shape to [1, height, width, channels] shape,
    #while tf.squeeze will simply eliminate all the dimensions with the number ‘1’,
    #which brings the raw_image back to the same 3D shape after reshaping

    raw_image = tf.expand_dims(raw_image, 0)
    raw_image = tf.image.resize_nearest_neighbor(raw_image, [height, width])
    raw_image = tf.squeeze(raw_image)

    #Finally, we just create the images and labels batch, using multiple threads to dequeue the examples
    #for training. The capacity is simply the capacity for the internal FIFO queue that exists
    #by default when you create a tf.train.batch, and a higher capacity is recommended if you have an unpredictable data input/output. This can data I/O stability can be seen through a summary created by default on TensorBoard when you use the tf.train.batch function. We also let allow_smaller_final_batch be True to use the last few examples even if they are insufficient to make a batch.
    images, raw_images, labels = tf.train.batch([image, raw_image, label],
                                                batch_size=batch_size,
                                                num_threads=4,
                                                capacity=4 * batch_size,
                                                allow_smaller_final_batch=True)

    return images, raw_images, labels
def _flow_preprocess(mv_record, height, width, input_size, bbox, train=True):
    mv_record_shape = tf.shape(mv_record)
    mv_bytes = 2 * tf.multiply(height, width)
    temporal_depth = input_size[-1]

    mv_input = tf.random_crop(mv_record, [temporal_depth, mv_bytes])

    mv_input = tf.reshape(mv_input, [-1])

    # split each list element into x and y components and stack depth wise
    image_shape = tf.stack([2 * temporal_depth, height, width])
    image_chunk = tf.reshape(mv_input, image_shape)
    image_chunk = tf.transpose(image_chunk, perm=[1, 2, 0])  #### [H,W,20]

    # cast image block as float
    image_chunk = tf.cast(image_chunk, tf.float32)

    def normalize_images(image_chunk):
        #mean subtraction and normalization
        mean, variance = tf.nn.moments(image_chunk,
                                       axes=[0,
                                             1])  ################    [H,W,20]
        float_image = tf.subtract(image_chunk,
                                  tf.expand_dims(tf.expand_dims(mean, 0),
                                                 0))  ################ -mean?

        return float_image

    image_chunk = normalize_images(image_chunk)

    # rescale between [0,1]
    image_chunk = tf.truediv(
        tf.subtract(image_chunk, tf.reduce_min(image_chunk)),
        tf.subtract(tf.reduce_max(image_chunk), tf.reduce_min(image_chunk)))

    # if bbox provided don't distort and just crop and resize
    #bbox_distortion = False if bbox is not None else True
    bbox_distortion = True

    resized_image_chunk, distorted_bbox = inception_preprocessing.preprocess_image(
        image_chunk,
        input_size[0],
        input_size[1],
        is_training=train,
        depth=2 * temporal_depth,
        color_distortion=False,
        bbox=bbox,
        bbox_distortion=bbox_distortion)
    float_image = tf.image.resize_images(resized_image_chunk,
                                         size=[input_size[0], input_size[1]])
    print float_image

    # convert to 3D tensor
    float_image = convert_2D_to_3D_tensor(float_image, input_size)

    return float_image, distorted_bbox
Ejemplo n.º 14
0
def batch_valid(start,batch_size): 
	data=[]
	for i in range(batch_size):		
		raw_image=validation_images[start+i]
		raw_image=np.reshape(raw_image,(20,64,3)) 
		raw_image1=tf.convert_to_tensor(raw_image,dtype=tf.float64)
		image = inception_preprocessing.preprocess_image(raw_image1, height=image_size, width=image_size, is_training=True)
		data.append(image)
	x1=tf.reshape(data,[batch_size,299,299,3])
	return x1
Ejemplo n.º 15
0
    def readFromTFRecords(self,
                          flag,
                          filename,
                          num_epochs,
                          img_shape,
                          batch_size,
                          num_threads,
                          min_after_dequeue=100):
        if flag:
            num_epochs = num_epochs
        else:
            num_epochs = 1
        filename_queue = tf.train.string_input_producer(filename,
                                                        num_epochs=num_epochs)

        # def read_and_decode(filename_queue, img_shape):
        reader = tf.TFRecordReader()
        _, serialized_example = reader.read(filename_queue)
        features = tf.parse_single_example(serialized_example,
                                           features={
                                               'image/class/label':
                                               tf.FixedLenFeature([],
                                                                  tf.int64),
                                               'image/encoded':
                                               tf.FixedLenFeature([],
                                                                  tf.string)
                                           })
        # image = tf.image.decode_jpeg(features['image/encoded'],3)
        # image = tf.reshape(image, img_shape)    # THIS IS IMPORTANT
        # image = tf.cast(image, tf.float32)

        image = tf.image.decode_jpeg(features['image/encoded'], 3)

        image = inception_preprocessing.preprocess_image(
            image,
            img_shape[0],
            img_shape[1],
            is_training=self.flag,
        )

        sparse_label = features['image/class/label']  # tf.int64
        # return image, sparse_label

        # image, sparse_label = read_and_decode(filename_queue, img_shape) # share filename_queue with multiple threads

        # tf.train.shuffle_batch internally uses a RandomShuffleQueue
        images, sparse_labels = tf.train.shuffle_batch(
            [image, sparse_label],
            batch_size=batch_size,
            num_threads=num_threads,
            min_after_dequeue=min_after_dequeue,
            capacity=min_after_dequeue + (num_threads + 1) * batch_size)

        return images, sparse_labels
def resnet_process_data_dir(work_dir, data_dir, model_dir):
    resNet_path = os.path.join(work_dir, 'ResNet')
    pickle_out = os.path.join(resNet_path, 'ResNet_preprocess.pk')
    if not os.path.exists(resNet_path):
        os.makedirs(resNet_path)
    if os.path.isfile(pickle_out):
        os.remove(pickle_out)
    print('ResNet preprocessing for ' + resNet_path)
    # Image directory info.
    img_files = sorted(
        [name for name in os.listdir(data_dir) if _is_img(name)])
    print('data_dir:{}'.format(data_dir))
    print('frames_num:{}'.format(len(img_files)))
    print('img_files:')
    print(img_files)
    img_list = []
    for pic in img_files:
        img_list.append(os.path.join(data_dir, pic))
    # Pre-process using ResNet.
    img_size = resnet_v2.resnet_v2.default_image_size
    resnet_v2_model = os.path.join(model_dir, 'resnet_v2_50.ckpt')
    print('resnet_v2_model:{}'.format(resnet_v2_model))
    with tf.Graph().as_default():
        processed_images = []
        for i, img in enumerate(img_list):
            # 读取图片并按照jpg格式转化为3通道的张量
            image = tf.image.decode_jpeg(tf.read_file(img), channels=3)
            # 预处理:双线性插值resize固定尺寸(224),中心裁剪0.875,float类型并且[0,1],normalization去均值化除以标准差
            processed_images.append(
                inception_preprocessing.preprocess_image(image,
                                                         img_size,
                                                         img_size,
                                                         is_training=False))
        processed_images = tf.convert_to_tensor(processed_images)

        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            # Return ResNet 2048 vector.
            logits, _ = resnet_v2.resnet_v2_50(processed_images,
                                               num_classes=None,
                                               is_training=False)
        init_fn = slim.assign_from_checkpoint_fn(
            resnet_v2_model, slim.get_variables_to_restore())

        with tf.Session() as sess:
            init_fn(sess)
            np_images, resnet_vectors = sess.run([processed_images, logits])
            resnet_vectors = resnet_vectors[:, 0, 0, :]
    print('form pickle_data......')
    # Save preprocessed data to pickle file.
    pickle_data = {'frame_resnet_vectors': resnet_vectors}

    pickle.dump(pickle_data, open(pickle_out, 'wb'))
    print('resNet finished!')
    print('{} has been dumped over!'.format(pickle_out))
Ejemplo n.º 17
0
    def build_model(self):
        self.input = tf.placeholder(tf.uint8, [None, None, 3])
        self.processed_image = inception_preprocessing.preprocess_image(
            self.input, self.height, self.width, is_training=False)
        self.processed_images = tf.expand_dims(self.processed_image, 0)

        with slim.arg_scope(inception_v3_arg_scope()):
            self.logits, self.end_points = inception_v3(self.processed_images,
                                                        num_classes=1001,
                                                        is_training=False)
        self.probabilities = tf.nn.softmax(self.logits)
Ejemplo n.º 18
0
def parser(filename, label, class_num, height, witdh, is_training):
    # with tf.gfile.GFile(filename, 'rb') as f:
    img = tf.read_file(filename)  # f.read()
    img = tf.image.decode_jpeg(img, channels=3)

    img_resized = inception_preprocessing.preprocess_image(
        img, height, witdh, is_training=is_training, add_image_summaries=False)
    # NOTE the inception_preprocessing will convert image scale to [-1,1]

    one_hot_label = tf.one_hot(label, class_num, 1, 0)
    # NOTE label should expand axis
    # one_hot_label = one_hot_label[tf.newaxis, tf.newaxis, :]
    return img_resized, one_hot_label
 def _inception_preprocess_patches(data):
   patches = []
   for _ in range(num_of_patches):
     patches.append(
         inception_preprocessing.preprocess_image(
             data["image"],
             resize_size[0],
             resize_size[1],
             is_training,
             add_image_summaries=False))
   patches = tf.stack(patches)
   data["image"] = patches
   return data
Ejemplo n.º 20
0
def load_batch(dataset, batch_size, height=img_height, width=img_width, is_training=True):
    '''
    Loads a batch for training.

    INPUTS:
    - dataset(Dataset): a Dataset class object that is created from the get_split function
    - batch_size(int): determines how big of a batch to train
    - height(int): the height of the image to resize to during preprocessing
    - width(int): the width of the image to resize to during preprocessing
    - is_training(bool): to determine whether to perform a training or evaluation preprocessing

    OUTPUTS:
    - images(Tensor): a Tensor of the shape (batch_size, height, width, channels) that contain one batch of images
    - labels(Tensor): the batch's labels with the shape (batch_size,) (requires one_hot_encoding).

    '''
    # First create the data_provider object
    data_provider = slim.dataset_data_provider.DatasetDataProvider(
        dataset,
        common_queue_capacity=24 + 3 * batch_size,
        common_queue_min=24)
    # Obtain the raw image using the get method
    raw_image, label = data_provider.get(['image', 'label'])
    # Perform the correct preprocessing for this image depending if it is training or evaluating
    image = inception_preprocessing.preprocess_image(raw_image, height, width, is_training)
    image = tf.reshape(image, [img_height, img_width, 3])
    label = tf.cast(label, tf.int32)
    """
    image = raw_image.eval(session=sess)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    image = cv2.resize(image, (img_width, img_height), 0, 0, cv2.INTER_LINEAR)
    image = image.astype(np.float32)
    image = np.multiply(image, 1.0 / 255.0)
    label = label.eval(session=sess)
    # Batch up the image and label
    return image, label
    """
   
    #As for the raw images, we just do a simple reshape to batch it up
    raw_image = tf.expand_dims(raw_image, 0)
    raw_image = tf.image.resize_nearest_neighbor(raw_image, [height, width])
    raw_image = tf.squeeze(raw_image)
    #Batch up the image by enqueing the tensors internally in a FIFO queue and dequeueing many elements with tf.train.batch.
    images, labels = tf.train.batch(
        [image, label],
        batch_size = batch_size,
        num_threads = 4,
        capacity = 4 * batch_size,
        allow_smaller_final_batch = True)
    print("images tensor data type:", tf.shape(images))
    return images, raw_image, labels
Ejemplo n.º 21
0
def getvector(imagedir):
  slim = tf.contrib.slim

  batch_size = 3
  image_size = v3.inception_v3.default_image_size

  url = "http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz"
  checkpoints_dir = os.getcwd()

  if not tf.gfile.Exists(checkpoints_dir + '/inception_v3.ckpt'):
    dataset_utils.download_and_uncompress_tarball(url, checkpoints_dir)

  with tf.Graph().as_default():
    # imagedir = '/home/jiexun/Desktop/Siraj/ImageChallenge/Necessary/train/cat.0.jpg'
    image_string = tf.read_file(imagedir)
    image = tf.image.decode_jpeg(image_string, channels=3)
    
    #Intentando reparar el resize
    '''
    print(image.shape)
    print(image)
    image=tf.cast(image, tf.float32)
    image=tf.image.resize_images(image, [299, 299])
    print(image.shape)
    print(image)
    image_jpg = tf.image.encode_jpeg(image)
    plt.imshow(image_jpg)
    '''
    #fin de parche

    processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
    processed_images = tf.expand_dims(processed_image, 0)

    # Create the model, use the default arg scope to configure the batch norm parameters.
    print('Inicializando el modelo InceptionV3...')
    with slim.arg_scope(v3.inception_v3_arg_scope()):
      vector, _ = v3.inception_v3(processed_images, num_classes=1001, is_training=False)

    init_fn = slim.assign_from_checkpoint_fn(os.path.join(checkpoints_dir, 'inception_v3.ckpt'),
                                             slim.get_model_variables('InceptionV3'))
    with tf.Session() as sess:
      init_fn(sess)
      np_image, vector = sess.run([image, vector])

    a = np.asarray([x for xs in vector for xss in xs for xsss in xss for x in xsss])
    np.reshape(a, (1, 2048))

  return a
Ejemplo n.º 22
0
def load_batch(dataset,
               batch_size,
               height=image_h,
               width=image_w,
               is_training=True):
    '''
    Loads a batch for training.

    INPUTS:
    - dataset(Dataset): a Dataset class object that is created from the get_split function
    - batch_size(int): determines how big of a batch to train
    - height(int): the height of the image to resize to during preprocessing
    - width(int): the width of the image to resize to during preprocessing
    - is_training(bool): to determine whether to perform a training or evaluation preprocessing

    OUTPUTS:
    - images(Tensor): a Tensor of the shape (batch_size, height, width, channels) that contain one batch of images
    - labels(Tensor): the batch's labels with the shape (batch_size,) (requires one_hot_encoding).

    '''
    #First create the data_provider object
    data_provider = slim.dataset_data_provider.DatasetDataProvider(
        dataset,
        #common_queue_capacity = 24 + 3 * batch_size,
        #common_queue_min = 24)
        common_queue_capacity=3 * batch_size,
        common_queue_min=2)

    #Obtain the raw image using the get method
    raw_image, label = data_provider.get(['image', 'label'])

    #Perform the correct preprocessing for this image depending if it is training or evaluating
    image = inception_preprocessing.preprocess_image(raw_image, height, width,
                                                     is_training)

    #As for the raw images, we just do a simple reshape to batch it up
    raw_image = tf.expand_dims(raw_image, 0)
    raw_image = tf.image.resize_nearest_neighbor(raw_image, [height, width])
    raw_image = tf.squeeze(raw_image)

    #Batch up the image by enqueing the tensors internally in a FIFO queue and dequeueing many elements with tf.train.batch.
    images, raw_images, labels = tf.train.batch([image, raw_image, label],
                                                batch_size=batch_size,
                                                num_threads=4,
                                                capacity=4 * batch_size,
                                                allow_smaller_final_batch=True)

    return images, raw_images, labels
Ejemplo n.º 23
0
def fname_to_image_tensor(fname, label, image_root, image_size):
    """ Loads and resizes an image given by FID. Pass-through the PID. """
    # Since there is no symbolic path.join, we just add a '/' to be sure.
    image_encoded = tf.read_file(tf.reduce_join([image_root, '/', fname]))

    # tf.image.decode_image doesn't set the shape, not even the dimensionality,
    # because it potentially loads animated .gif files. Instead, we use either
    # decode_jpeg or decode_png, each of which can decode both.
    # Sounds ridiculous, but is true:
    # https://github.com/tensorflow/tensorflow/issues/9356#issuecomment-309144064
    image_decoded = tf.image.decode_jpeg(image_encoded, channels=3)
    #image_resized = tf.image.resize_images(image_decoded, image_size)

    # Preprocess images
    image_resized = inception_preprocessing.preprocess_image(image_decoded, image_size[0], image_size[1],
            is_training=True)

    return image_resized, fname, label
Ejemplo n.º 24
0
  def _dataset_parser(self, serialized_proto):
    """Parse an Imagenet record from value."""
    keys_to_features = {
        'image/encoded':
            tf.FixedLenFeature((), tf.string, default_value=''),
        'image/format':
            tf.FixedLenFeature((), tf.string, default_value='jpeg'),
        'image/class/label':
            tf.FixedLenFeature([], dtype=tf.int64, default_value=-1),
        'image/class/text':
            tf.FixedLenFeature([], dtype=tf.string, default_value=''),
        'image/object/bbox/xmin':
            tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/ymin':
            tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/xmax':
            tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/ymax':
            tf.VarLenFeature(dtype=tf.float32),
        'image/object/class/label':
            tf.VarLenFeature(dtype=tf.int64),
    }

    features = tf.parse_single_example(serialized_proto, keys_to_features)

    bbox = None

    image = features['image/encoded']
    image = tf.image.decode_jpeg(image, channels=3)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)

    image = inception_preprocessing.preprocess_image(
        image=image,
        output_height=self.hparams.image_size,
        output_width=self.hparams.image_size,
        is_training=self.is_training,
        # If eval_from_hub, do not scale the images during preprocessing.
        scaled_images=not self.eval_from_hub,
        bbox=bbox)

    label = tf.cast(
        tf.reshape(features['image/class/label'], shape=[]), dtype=tf.int32)

    return image, label
Ejemplo n.º 25
0
def getfeatures(file_name):
    # Extract the features from InceptionNet (The features of Mixed_6a layer)
    slim = tf.contrib.slim
    image_size = inception_v4.inception_v4.default_image_size
    checkpoints_dir = os.getcwd()
    with tf.Graph().as_default():
        image_path = tf.read_file(file_name)
        image = tf.image.decode_jpeg(image_path, channels=3)
        processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
        processed_images  = tf.expand_dims(processed_image, 0)
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            vector = inception_v4.inception_v4(processed_images, num_classes=1001, is_training=False)
        init_fn = slim.assign_from_checkpoint_fn(os.path.join(checkpoints_dir, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4'))
        with tf.Session() as sess:
            init_fn(sess)
            np_image, vector = sess.run([image, vector])
        vector = np.asarray(vector)
        # print vector[1]['Mixed_6a'].shape
        return vector[1]['Mixed_6a']
def load_batch(dataset, batch_size=batch_size, height=image_size, width=image_size, is_training=True):
    """Loads a single batch of data for training.

    Args:
      dataset: The dataset to load, created in the get_split function.
      batch_size: The number of images in the batch.
      height(int): int value that is the size the image will be resized to during preprocessing
      width: The size that the image will be resized to during preprocessing
      is_training: Whether or not we're currently training or evaluating.

    Returns:
      images: A Tensor of size [batch_size, height, width, channels(3)], image samples that have been preprocessed, that contain one batch of images.
      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 (requires one-hot encodings)
    """

    # First create the data_provider object
    data_provider = slim.dataset_data_provider.DatasetDataProvider(
        dataset,
        common_queue_capacity=32,
        common_queue_min=8)

    # Obtain the raw image using the get method
    raw_image, label = data_provider.get(['image', 'label'])

    # Perform the correct preprocessing for this image depending if it is training or evaluating
    image = inception_preprocessing.preprocess_image(raw_image, height, width, is_training)

    # Preprocess the image for display purposes.
    raw_image = tf.expand_dims(raw_image, 0)
    raw_image = tf.image.resize_nearest_neighbor(raw_image, [height, width])
    raw_image = tf.squeeze(raw_image)

    # Batch up the images by enqueing the tensors internally in a FIFO queue and dequeueing many elements with tf.train.batch.
    images, raw_images, labels = tf.train.batch(
        [image, raw_image, label],
        batch_size=batch_size,
        num_threads=4,
        capacity=4 * batch_size,
        allow_smaller_final_batch=True)

    return images, raw_images, labels
def get_batch(image, label, image_W, image_H, batch_size, capacity):
    image = tf.cast(image, tf.string)
    label = tf.cast(label, tf.int32)
    # make an input queue
    input_queue = tf.train.slice_input_producer([image, label], shuffle=False)
    label = input_queue[1]
    image_contents = tf.read_file(input_queue[0])
    image = tf.image.decode_jpeg(image_contents, channels=3)
    # 数据增强
    image = preprocess_image(image, image_H, image_W, is_training=True)
    # 标准化,使图片的均值为0,方差为1
    image = tf.image.per_image_standardization(image)
    image_batch, label_batch = tf.train.batch([image, label],
                                              batch_size=batch_size,
                                              num_threads=64,
                                              capacity=capacity)
    tf.summary.image("input_img", image_batch, max_outputs=5)
    label_batch = tf.reshape(label_batch, [batch_size])
    image_batch = tf.cast(image_batch, tf.float32)
    return image_batch, label_batch
Ejemplo n.º 28
0
def clone_fn(images):
    images = tf.reshape(images, [IMAGE_SIZE, IMAGE_SIZE, 3])
    images = preprocess_image(images,
                              IMAGE_SIZE,
                              IMAGE_SIZE,
                              is_training=False)
    images = tf.reshape(images, [FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 3])

    logits, _ = inference(images,
                          num_classes=36,
                          for_training=False,
                          restore_logits=True,
                          scope=None)

    predictions = {
        'classes': tf.argmax(logits, axis=1, name='classes'),
        'probabilities': tf.nn.softmax(logits, name='probabilities')
    }

    return predictions
Ejemplo n.º 29
0
def record_parser(value, is_training):
    """Parse an ImageNet record from `value`."""
    keys_to_features = {
        'image/encoded':
        tf.FixedLenFeature((), tf.string, default_value=''),
        'image/format':
        tf.FixedLenFeature((), tf.string, default_value='jpeg'),
        'image/class/label':
        tf.FixedLenFeature([], dtype=tf.int64, default_value=-1),
        'image/class/text':
        tf.FixedLenFeature([], dtype=tf.string, default_value=''),
        'image/object/bbox/xmin':
        tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/ymin':
        tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/xmax':
        tf.VarLenFeature(dtype=tf.float32),
        'image/object/bbox/ymax':
        tf.VarLenFeature(dtype=tf.float32),
        'image/object/class/label':
        tf.VarLenFeature(dtype=tf.int64),
    }

    parsed = tf.parse_single_example(value, keys_to_features)

    image = tf.image.decode_image(
        tf.reshape(parsed['image/encoded'], shape=[]), _NUM_CHANNELS)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)

    image = inception_preprocessing.preprocess_image(
        image=image,
        height=_DEFAULT_IMAGE_SIZE,
        width=_DEFAULT_IMAGE_SIZE,
        is_training=is_training)

    label = tf.cast(tf.reshape(parsed['image/class/label'], shape=[]),
                    dtype=tf.int32)

    return image, tf.one_hot(label, _LABEL_CLASSES)
Ejemplo n.º 30
0
def process_image(image):
    root_dir = "images/"
    filename = root_dir + image
    with open(filename, "rb") as f:
        image_str = f.read()
        
    if image.endswith('jpg'):
        raw_image = tf.image.decode_jpeg(image_str, channels=3)
    elif image.endswith('png'):
        raw_image = tf.image.decode_png(image_str, channels=3)
    else: 
        print("Image must be either jpg or png")
        return 
    
    image_size = 299 # ImageNet image size, different models may be sized differently
    processed_image = inception_preprocessing.preprocess_image(raw_image, image_size,
                                                             image_size, is_training=False)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        raw_image, processed_image = sess.run([raw_image, processed_image])
        
    return raw_image, processed_image.reshape(-1, 299, 299, 3)
Ejemplo n.º 31
0
def preprocess_raw_bytes(image_bytes, is_training=False, bbox=None):
    """Preprocesses a raw JPEG image.

  This implementation is shared in common between train/eval pipelines,
  and when serving the model.

  Args:
    image_bytes: A string Tensor, containing the encoded JPEG.
    is_training: Whether or not to preprocess for training.
    bbox:        In inception preprocessing, this bbox can be used for cropping.

  Returns:
    A 3-Tensor [height, width, RGB channels] of type float32.
  """

    image = tf.image.decode_jpeg(image_bytes, channels=3)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)

    if FLAGS.preprocessing == 'vgg':
        image = vgg_preprocessing.preprocess_image(
            image=image,
            output_height=FLAGS.height,
            output_width=FLAGS.width,
            is_training=is_training,
            resize_side_min=_RESIZE_SIDE_MIN,
            resize_side_max=_RESIZE_SIDE_MAX)
    elif FLAGS.preprocessing == 'inception':
        image = inception_preprocessing.preprocess_image(
            image=image,
            output_height=FLAGS.height,
            output_width=FLAGS.width,
            is_training=is_training,
            bbox=bbox)
    else:
        assert False, 'Unknown preprocessing type: %s' % FLAGS.preprocessing
    return image
def getfeatures(file_name):
    # Extract the features from InceptionNet (The features of Mixed_6a layer)
    slim = tf.contrib.slim
    image_size = inception_v4.inception_v4.default_image_size
    checkpoints_dir = os.getcwd()
    with tf.Graph().as_default():
        image_path = tf.read_file(file_name)
        image = tf.image.decode_jpeg(image_path, channels=3)
        processed_image = inception_preprocessing.preprocess_image(
            image, image_size, image_size, is_training=False)
        processed_images = tf.expand_dims(processed_image, 0)
        with slim.arg_scope(inception_v4.inception_v4_arg_scope()):
            vector = inception_v4.inception_v4(processed_images,
                                               num_classes=1001,
                                               is_training=False)
        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(checkpoints_dir, 'inception_v4.ckpt'),
            slim.get_model_variables('InceptionV4'))
        with tf.Session() as sess:
            init_fn(sess)
            np_image, vector = sess.run([image, vector])
        vector = np.asarray(vector)
        # print vector[1]['Mixed_6a'].shape
        return vector[1]['Mixed_6a']