def LoadFromNpys(self, dir, classes): self.imgDataWrappers = np.empty(0, dtype=ImgDataWrapperMultiTypesMemory) npysImgs = CommonUtil.GetFileFromThisRootDir( os.path.join(dir, "arrImgs"), ".npy") npysMasks = CommonUtil.GetFileFromThisRootDir( os.path.join(dir, "masks"), ".npy") if len(npysImgs) != len(npysMasks): print( "WARNING: Count of img inputs is different from count of mask inputs." ) for i in range(len(npysImgs)): # print("Loading",i,":") # print(" " + npysImgs[i]) # print(" " + npysMasks[i]) imgs = np.load(npysImgs[i]) masks = np.load(npysMasks[i]) # print(imgs.shape) # print(masks.shape) if imgs is None: raise Exception("Imgs is none. No img has been read.") if masks is None: raise Exception("Masks is none. No mask has been read.") self.imgDataWrappers = np.insert(self.imgDataWrappers, len(self.imgDataWrappers), \ ImgDataWrapperMultiTypesMemory(imgs, masks, classes, imgDataFmt=imgs.dtype, maskDataFmt=masks.dtype, isMaskOneHot=True), \ axis=0)
def SaveToNpys(self, dir): countWrappers = len(self.imgDataWrappers) digitWrapperCount = len(str(countWrappers - 1)) dirImg = os.path.join(dir, "arrImgs") dirMask = os.path.join(dir, "masks") CommonUtil.Mkdir(dirImg) CommonUtil.Mkdir(dirMask) for i in range(countWrappers): wrapper = self.imgDataWrappers[i] arrImgs = wrapper.arrImgs masks = wrapper.masks fn = str(i).zfill(digitWrapperCount) + ".npy" pathImg = os.path.join(dirImg, fn) pathMask = os.path.join(dirMask, fn) np.save(pathImg, arrImgs) np.save(pathMask, masks)
def _check_value_type(_input, _value): try: if _input.type == bool: return CommonUtil.get_boolean_value(_value) else: return _input.type(_value) except TypeError as _: raise _
def InitFromImgsAndMasks(self, imgs, masks, classes, dirSave, resize=None, preproc=None, imgDataFmt=float, maskDataFmt=int, isMaskOneHot=False): self.pathsImg = None self.pathsMasks = None if resize is not None: # Use pure resizing to preserve data matchiing imgs = transform.resize(imgs, resize + (self.imgs.shape[2], ), order=0, clip=True, preserve_range=True, anti_aliasing=False) masks = transform.resize(masks, resize + (self.masks.shape[2], ), order=0, clip=True, preserve_range=True, anti_aliasing=False) gc.collect() if preproc is not None: imgs, masks = preproc(imgs, masks, classes) gc.collect() imgs = imgs.astype(dtype=imgDataFmt) masks = masks.astype(dtype=maskDataFmt) # One-hot encoding target if classes <= 1: raise Exception("Class count = 1. Unable to run! ") if not isMaskOneHot: maxClass = np.max(np.unique(masks)) if maxClass > classes: raise Exception("Not enough classes! MaxClassValue: " + str(maxClass)) masks = CommonUtil.PackIntoOneHot(masks, classes) gc.collect() if np.any(np.isnan(imgs)): raise Exception("NAN Warning!") if np.any(np.isnan(masks)): raise Exception("NAN Warning!") # Save to npys self.__SaveToNPYs(dirSave, imgs, masks)
def __init__(self, arrImgs, masks, classes, resize=None, preproc=None, imgDataFmt=float, maskDataFmt=int, isMaskOneHot=False): self.arrImgs = arrImgs # ndarray[H, W, Type, Slices] self.masks = masks if resize is not None: # Use pure resizing to preserve data matchiing self.arrImgs = transform.resize(self.arrImgs, (self.arrImgs.shape[0], ) + resize + (self.arrImgs.shape[3], ), order=0, clip=True, preserve_range=True, anti_aliasing=False) self.masks = transform.resize(self.masks, resize + (self.masks.shape[2], ), order=0, clip=True, preserve_range=True, anti_aliasing=False) gc.collect() if preproc is not None: self.arrImgs, self.masks = preproc(self.arrImgs, self.masks, classes) gc.collect() self.arrImgs = self.arrImgs.astype(dtype=imgDataFmt) self.masks = self.masks.astype(dtype=maskDataFmt) # One-hot encoding target if classes <= 1: raise Exception("Class count = 1. Unable to run! ") if not isMaskOneHot: maxClass = np.max(np.unique(self.masks)) if maxClass > classes: raise Exception("Not enough classes! MaxClassValue: " + str(maxClass)) self.masks = CommonUtil.PackIntoOneHot(self.masks, classes) gc.collect() if np.any(np.isnan(self.arrImgs)): raise Exception("NAN Warning!") if np.any(np.isnan(self.masks)): raise Exception("NAN Warning!")
def __SaveToNPYs(self, dirWrapper, imgs, masks): self.pathsImg = [] self.pathsMasks = [] suffix = ".npy" dirData = os.path.join(dirWrapper, "data") dirMasks = os.path.join(dirWrapper, "masks") CommonUtil.Mkdir(dirData) CommonUtil.Mkdir(dirMasks) slices = imgs.shape[-1] digitCount = len(str(slices)) for i in range(slices): filename = str(i).zfill(digitCount) + suffix pathImg = os.path.join(dirData, filename) pathMask = os.path.join(dirMasks, filename) np.save(pathImg, imgs[..., i]) self.pathsImg += [pathImg] np.save(pathMask, masks[..., i]) self.pathsMasks += [pathMask]
def run(self): super(CoAPSniffAttack, self).run() packets = CoAPScanner().scan( self.timeout, self.interface, output_pcap_filename="CoAP_" + CommonUtil.get_current_datetime_for_filename_format() if self.save_output else None) for packet in packets: self.logger.info(packet) return packets
def run(self): self.input_check() # Do the necessary checks super(MQTTSniffAttack, self).run() packets = MQTTScanner().scan( self.timeout, self.interface, output_pcap_filename="MQTT_" + CommonUtil.get_current_datetime_for_filename_format() if self.save_output else None) for packet in packets: self.logger.info(packet) return packets
def __LoadFromNPYs(self, dirWrapper): dirData = os.path.join(dirWrapper, "data") dirMasks = os.path.join(dirWrapper, "masks") self.pathsImg = CommonUtil.GetFileFromThisRootDir(dirData, ".npy") self.pathsMasks = CommonUtil.GetFileFromThisRootDir(dirMasks, ".npy")
def create_file_name(): # store the captured packets return os.path.dirname( os.path.abspath(__file__) ) + "/../../captured_packets/BLE_" + CommonUtil.get_current_datetime_for_filename_format( ) + ".pcap"