def preprocess_data(self, images, is_training): """Preprocesses raw images for either training or inference. Args: images: A 4-D float32 `Tensor` holding images to preprocess. is_training: Boolean, whether or not we're in training. Returns: data_preprocessed: data after the preprocessor. """ config = self._config height = config.data.height width = config.data.width min_scale = config.data.augmentation.minscale max_scale = config.data.augmentation.maxscale p_scale_up = config.data.augmentation.proportion_scaled_up aug_color = config.data.augmentation.color fast_mode = config.data.augmentation.fast_mode crop_strategy = config.data.preprocessing.eval_cropping preprocessed_images = preprocessing.preprocess_images( images, is_training, height, width, min_scale, max_scale, p_scale_up, aug_color=aug_color, fast_mode=fast_mode, crop_strategy=crop_strategy) return preprocessed_images
def preprocess(self, inputs): """preprocessing. Outputs of this function can be passed to loss or postprocess functions. Args: preprocessed_inputs: A float32 tensor with shape [batch_size, height, width, num_channels] representing a batch of images. Returns: prediction_dict: A dictionary holding prediction tensors to be passed to the Loss or Postprocess functions. """ preprocessed_inputs = preprocessing.preprocess_images( inputs, self._default_image_size, self._default_image_size, resize_side_min=self._fixed_resize_side, is_training=self._is_training, border_expand=False, normalize=False, preserving_aspect_ratio_resize=False) preprocessed_inputs = tf.cast(preprocessed_inputs, tf.float32) #tf.cast()数据类型转化 return preprocessed_inputs
def preprocess(self, inputs): preprocessed_inputs = preprocessing.preprocess_images( inputs, self._default_image_size, self._default_image_size, resize_side_min=self._fixed_resize_side, is_training=self._is_training, border_expand=True, normalize=False, preserving_aspect_ratio_resize=False) preprocessed_inputs = tf.cast(preprocessed_inputs, tf.float32) return preprocessed_inputs
def load_batch(dataset, batch_size, height, width, num_classes): data_provider = slim.dataset_data_provider.DatasetDataProvider(dataset) image, label = data_provider.get(['image', 'label']) # Border expand and resize image = preprocessing.preprocess_images(image, output_height=height, output_width=width, border_expand=True, normalize=True) inputs, labels = tf.train.batch([image, label], batch_size=batch_size, allow_smaller_final_batch=True) labels = slim.one_hot_encoding(labels, num_classes) return inputs, labels
def preprocess(self, inputs): """ 利用preprocessing.py中的数据处理函数对输入数据进行处理 输入 [batch_size,height, width, num_channels] a batch of images. 输出 tensors [batch_size,height, width, num_channels] """ # 调用批处理函数进行处理 with tf.variable_scope('Preprocess'): preprocessed_inputs = preprocessing.preprocess_images( inputs, self._default_image_size, self._default_image_size, resize_side_min=self._fixed_resize_side, is_training=self._is_training, border_expand=False, normalize=False, preserving_aspect_ratio_resize=False) # 转化数据类型为 float32 preprocessed_inputs = tf.cast(preprocessed_inputs, tf.float32) return preprocessed_inputs
# Loading the dataset path = "../" x_train, y_train, x_validation, y_validation, classes = filehandler.import_dataset( path) # Example of a picture """index = 20 plt.imshow(x_train[index]) plt.show() print("y = " + str(y_train[:, index]) + ", it's a '" + classes[numpy.squeeze(y_train[:, index])].decode("utf-8") + "' picture.") """ # preprocessing the dataset train_flat, validation_flat = preprocessing.preprocess_images(x_train, x_validation) # creating our model input_shape = train_flat.shape[0] log_regression = log_reg.LogisticRegression(input_shape) # training our model epochs = 300 learning_rate = 0.001 # if the probability is below 0.3 or above 0.7, the sample is "correctly predicted" recognition_threshold = 0.3 history = log_regression.train(train_flat, y_train, validation_flat, y_validation, epochs, learning_rate, recognition_threshold)