def get_raw_image_set(self, with_label): resource_path = os.path.join(os.path.split(__file__)[0], "../../resources") if with_label: image_folder = os.path.join(resource_path, "cat_dog") else: image_folder = os.path.join(resource_path, "cat_dog/*") image_set = ImageSet.read(image_folder, with_label=with_label, sc=get_spark_context(), one_based_label=False) return image_set
def input_fn(mode): import os resource_path = os.path.join( os.path.split(__file__)[0], "../resources") if mode == tf.estimator.ModeKeys.TRAIN: image_folder = os.path.join(resource_path, "cat_dog") image_set = ImageSet.read(image_folder, with_label=True, sc=self.sc, one_based_label=False) transformer = ChainedPreprocessing([ ImageResize(256, 256), ImageRandomCrop(224, 224, True), ImageMatToTensor(format="NHWC"), ImageSetToSample(input_keys=["imageTensor"], target_keys=["label"]) ]) image_set = image_set.transform(transformer) dataset = TFDataset.from_image_set(image_set, image=(tf.float32, [224, 224, 3]), label=(tf.int32, [1]), batch_size=8) elif mode == tf.estimator.ModeKeys.EVAL: image_folder = os.path.join(resource_path, "cat_dog") image_set = ImageSet.read(image_folder, with_label=True, sc=self.sc, one_based_label=False) transformer = ChainedPreprocessing([ ImageResize(256, 256), ImageRandomCrop(224, 224, True), ImageMatToTensor(format="NHWC"), ImageSetToSample(input_keys=["imageTensor"], target_keys=["label"]) ]) image_set = image_set.transform(transformer) dataset = TFDataset.from_image_set(image_set, image=(tf.float32, [224, 224, 3]), label=(tf.int32, [1]), batch_per_thread=8) else: image_folder = os.path.join(resource_path, "cat_dog/*/*") image_set = ImageSet.read(image_folder, with_label=False, sc=self.sc, one_based_label=False) transformer = ChainedPreprocessing([ ImageResize(256, 256), ImageRandomCrop(224, 224, True), ImageMatToTensor(format="NHWC"), ImageSetToSample(input_keys=["imageTensor"]) ]) image_set = image_set.transform(transformer) dataset = TFDataset.from_image_set(image_set, image=(tf.float32, [224, 224, 3]), batch_per_thread=8) return dataset
parser = OptionParser() parser.add_option("--image", type=str, dest="img_path", help="The path where the images are stored, " "can be either a folder or an image path") parser.add_option("--model", type=str, dest="model_path", help="Path to the TensorFlow model file") (options, args) = parser.parse_args(sys.argv) sc = init_nncontext("OpenVINO Object Detection Inference Example") images = ImageSet.read(options.img_path, sc, resize_height=600, resize_width=600).get_image().collect() input_data = np.concatenate( [image.reshape((1, 1) + image.shape) for image in images], axis=0) model_path = options.model_path model = InferenceModel() model.load_openvino(model_path, weight_path=model_path[:model_path.rindex(".")] + ".bin") predictions = model.predict(input_data) # Print the detection result of the first image. print(predictions[0]) print("finished...") sc.stop()