def preprocess_coco_for_ssdresnet34(data_dir, preprocessed_data_dir, formats, overwrite=False): def loader(file): image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = np.array( cv2.resize(image, (1200, 1200), interpolation=cv2.INTER_LINEAR)).astype(np.float32) image = image.transpose((2, 0, 1)) return image def quantizer(image): # Dynamic range of image is [-2.64064, 2.64064] based on calibration cache. max_abs = 2.64064 image_int8 = image.clip(-max_abs, max_abs) / max_abs * 127.0 return image_int8.astype(dtype=np.int8, order='C') preprocessor = ImagePreprocessor(loader, quantizer) # Preprocess validation set. preprocessor.run( os.path.join(data_dir, "coco", "val2017"), os.path.join(preprocessed_data_dir, "coco", "val2017", "SSDResNet34"), "data_maps/coco/val_map.txt", formats, overwrite)
def preprocess_imagenet_for_resnet50(data_dir, preprocessed_data_dir, formats, overwrite=False): """Proprocess the raw images for inference.""" def loader(file): """Resize and crop image to required dims and return as FP32 array.""" image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) w, h = (224, 224) image = resize_with_aspectratio(image, h, w) image = center_crop(image, h, w) image = np.asarray(image, dtype='float32') # Transpose. image = image.transpose([2, 0, 1]) return image def quantizer(image): """Return quantized INT8 image of input FP32 image.""" return np.clip(image, -128.0, 127.0).astype(dtype=np.int8, order='C') preprocessor = ImagePreprocessor(loader, quantizer) # Preprocess validation set. preprocessor.run( os.path.join(data_dir, "imagenet"), os.path.join(preprocessed_data_dir, "imagenet", "ResNet50"), "data_maps/imagenet/val_map.txt", formats, overwrite)
def preprocess_imagenet_for_resnet50(data_dir, preprocessed_data_dir, formats, overwrite=False, cal_only=False, val_only=False): def loader(file): img = Image.open(file) img = img.convert('RGB') img = resize_with_aspectratio(img, 256) img = center_crop(img, (224, 224)) img = np.asarray(img, dtype='float32') img /= 255.0 mean = np.array([0.485,0.456,0.406], dtype=np.float32) std = np.array([0.229,0.224,0.225], dtype=np.float32) img = (img - mean) / std img = img.transpose([2, 0, 1]) #img = np.asarray(img.reshape((3,224,224)), dtype='float32') return img def quantizer(image): max_abs = 2.64064 image_int8 = image.clip(-max_abs, max_abs) / max_abs * 127.0 return image_int8.astype(dtype=np.int8, order='C') preprocessor = ImagePreprocessor(loader, quantizer) if not val_only: # Preprocess calibration set. FP32 only because calibrator always takes FP32 input. preprocessor.run(os.path.join(data_dir, "imagenet"), os.path.join(preprocessed_data_dir, "imagenet", "ResNet50"), "data_maps/imagenet/cal_map.txt", ["fp32"], overwrite) if not cal_only: # Preprocess validation set. preprocessor.run(os.path.join(data_dir, "imagenet"), os.path.join(preprocessed_data_dir, "imagenet", "ResNet50"), "data_maps/imagenet/val_map.txt", formats, overwrite)
def preprocess_coco_for_ssdresnet34(data_dir, preprocessed_data_dir, formats, overwrite=False, cal_only=False, val_only=False): def loader(file): image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = np.array( cv2.resize(image, (1200, 1200), interpolation=cv2.INTER_LINEAR)).astype(np.float32) image = image.transpose((2, 0, 1)) image /= 255.0 # Normalize image. means = np.array([0.485, 0.456, 0.406], dtype=np.float32)[:, np.newaxis, np.newaxis] std = np.array([0.229, 0.224, 0.225], dtype=np.float32)[:, np.newaxis, np.newaxis] image = (image - means) / std return image def quantizer(image): # Dynamic range of image is [-2.64064, 2.64064] based on calibration cache. max_abs = 2.64064 image_int8 = image.clip(-max_abs, max_abs) / max_abs * 127.0 return image_int8.astype(dtype=np.int8, order='C') preprocessor = ImagePreprocessor(loader, quantizer) if not val_only: # Preprocess calibration set. FP32 only because calibrator always takes FP32 input. preprocessor.run( os.path.join(data_dir, "coco", "train2017"), os.path.join(preprocessed_data_dir, "coco", "train2017", "SSDResNet34"), "data_maps/coco/cal_map.txt", ["fp32"], overwrite) if not cal_only: # Preprocess validation set. preprocessor.run( os.path.join(data_dir, "coco", "val2017"), os.path.join(preprocessed_data_dir, "coco", "val2017", "SSDResNet34"), "data_maps/coco/val_map.txt", formats, overwrite)
def preprocess_imagenet_for_resnet50(data_dir, preprocessed_data_dir, formats, overwrite=False, cal_only=False, val_only=False): """Proprocess the raw images for inference.""" def loader(file): """Resize and crop image to required dims and return as FP32 array.""" img = Image.open(file) img = img.convert('RGB') img = resize_with_aspectratio(img, 202) img = center_crop(img, (176, 176)) img = np.asarray(img, dtype='float32') img /= 255.0 mean = np.array([0.485, 0.456, 0.406], dtype=np.float32) std = np.array([0.229, 0.224, 0.225], dtype=np.float32) img = (img - mean) / std img = img.transpose([2, 0, 1]) #img = np.asarray(img.reshape((3,224,224)), dtype='float32') return img def quantizer(image): """Return quantized INT8 image of input FP32 image.""" scale = struct.unpack('!f', bytes.fromhex('3caa5293'))[0] image_int8 = (image / scale).clip(-127.0, 127.0) return image_int8.astype(dtype=np.int8, order='C') preprocessor = ImagePreprocessor(loader, quantizer) if not val_only: # Preprocess calibration set. FP32 only because calibrator always takes FP32 input. preprocessor.run( os.path.join(data_dir, "imagenet"), os.path.join(preprocessed_data_dir, "imagenet", "ResNet50"), "data_maps/imagenet/cal_map.txt", ["fp32"], overwrite) if not cal_only: # Preprocess validation set. preprocessor.run( os.path.join(data_dir, "imagenet"), os.path.join(preprocessed_data_dir, "imagenet", "ResNet50"), "data_maps/imagenet/val_map.txt", formats, overwrite)
def preprocess_imagenet_for_resnet50(data_dir, preprocessed_data_dir, formats, overwrite=False, cal_only=False, val_only=False): def loader(file): image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) w, h = (224, 224) image = resize_with_aspectratio(image, h, w) image = center_crop(image, h, w) image = np.asarray(image, dtype='float32') # Normalize image. means = np.array([123.68, 116.78, 103.94], dtype=np.float32) image -= means # Transpose. image = image.transpose([2, 0, 1]) return image def quantizer(image): return np.clip(image, -128.0, 127.0).astype(dtype=np.int8, order='C') preprocessor = ImagePreprocessor(loader, quantizer) if not val_only: # Preprocess calibration set. FP32 only because calibrator always takes FP32 input. preprocessor.run( os.path.join(data_dir, "imagenet"), os.path.join(preprocessed_data_dir, "imagenet", "ResNet50"), "data_maps/imagenet/cal_map.txt", ["fp32"], overwrite) if not cal_only: # Preprocess validation set. preprocessor.run( os.path.join(data_dir, "imagenet"), os.path.join(preprocessed_data_dir, "imagenet", "ResNet50"), "data_maps/imagenet/val_map.txt", formats, overwrite)
def preprocess_coco_for_ssdmobilenet(data_dir, preprocessed_data_dir, formats, overwrite=False, cal_only=False, val_only=False): def loader(file): image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = np.array( cv2.resize(image, (300, 300), interpolation=cv2.INTER_LINEAR)).astype(np.float32) image = image.transpose((2, 0, 1)) image = (2.0 / 255.0) * image - 1.0 return image def quantizer(image): # Dynamic range of image is [-1.0, 1.0] image_int8 = image * 127.0 return image_int8.astype(dtype=np.int8, order='C') preprocessor = ImagePreprocessor(loader, quantizer) if not val_only: # Preprocess calibration set. FP32 only because calibrator always takes FP32 input. preprocessor.run( os.path.join(data_dir, "coco", "train2017"), os.path.join(preprocessed_data_dir, "coco", "train2017", "SSDMobileNet"), "data_maps/coco/cal_map.txt", ["fp32"], overwrite) if not cal_only: # Preprocess validation set. preprocessor.run( os.path.join(data_dir, "coco", "val2017"), os.path.join(preprocessed_data_dir, "coco", "val2017", "SSDMobileNet"), "data_maps/coco/val_map.txt", formats, overwrite)