Esempio n. 1
0
 def _transmit(connection, command, command_vars=None):
     """Returns: data as a byte list"""
     if command_vars is None: command_vars = []
     command_desc = command[0]
     full_command = command[1] + command_vars
     sw = [0, 0]
     try:
         data, sw[0], sw[1] = connection.transmit(full_command)
     except(AttributeError, IndexError):
         # Connection lost
         raise exceptions.ConnectionLostException
     response_code = toHexString(sw)
     if response_code == "64 00":  # card execution error
         raise exceptions.FailedException(command_desc)
     elif response_code == "67 00":  # wrong length
         raise exceptions.NotSupportedException(command_desc)
     elif response_code == "68 00":  # invalid class (CLA) byte
         raise exceptions.NotSupportedException(command_desc)
     elif response_code == "69 81":  # Command incompatible.
         raise exceptions.FailedException(command_desc)
     elif response_code == "69 82":  # Security status not satisfied.
         raise exceptions.FailedException(command_desc)
     elif response_code == "69 86":  # Command not allowed.
         raise exceptions.FailedException(command_desc)
     elif response_code == "6A 81":  # invalid instruction (INS) byte
         raise exceptions.NotSupportedException(command_desc)
     elif response_code == "6A 82":  # File not found / Addressed block or byte does not exist.
         raise exceptions.FailedException(command_desc)
     elif response_code.startswith("6C"):  # wrong length
         raise exceptions.NotSupportedException(command_desc)
     elif response_code != "90 00":
         raise exceptions.UnexpectedErrorCodeException(response_code, command_desc, sw[0], sw[1])
     return data
Esempio n. 2
0
def squeezenet1_1():
    r'''
    Return SqueezeNet v1.1 layers which can be supported.
    '''
    conv_layer = ['conv_0',
                  'sqz_1', 'exp_1_1', 'exp_1_3',
                  'sqz_2', 'exp_2_1', 'exp_2_3',
                  'sqz_3', 'exp_3_1', 'exp_3_3',
                  'sqz_4', 'exp_4_1', 'exp_4_3',
                  'sqz_5', 'exp_5_1', 'exp_5_3',
                  'sqz_6', 'exp_6_1', 'exp_6_3',
                  'sqz_7', 'exp_7_1', 'exp_7_3',
                  'sqz_8', 'exp_8_1', 'exp_8_3',
                  'conv_c']
    pool_layer = ['pool1', 'pool2', 'pool3', 'pool4']

    tv_sqznet1_1 = torchvision.models.squeezenet1_1(pretrained=False, progress=True)
    tv_sqznet1_1.features[2].padding = 2
    tv_sqznet1_1.features[5].padding = 2
    tv_sqznet1_1.features[8].padding = 2
    layers = nn.Sequential()

    conv_layer.reverse()
    pool_layer.reverse()

    # Feature Extraction Layers
    for feature in tv_sqznet1_1.features:
        if isinstance(feature, nn.Conv2d):
            layers.add_module(conv_layer.pop(), feature)
        elif isinstance(feature, nn.ReLU):
            layers.add_module('ReLU', feature)
        elif isinstance(feature, nn.MaxPool2d):
            layers.add_module(pool_layer.pop(), feature)
        elif isinstance(feature, torchvision.models.squeezenet.Fire):
            layers.add_module(conv_layer.pop(), feature.squeeze)
            layers.add_module('ReLU', feature.squeeze_activation)
            layers.add_module(conv_layer.pop(), feature.expand1x1)
            layers.add_module('ReLU', feature.expand1x1_activation)
            layers.add_module(conv_layer.pop(), feature.expand3x3)
            layers.add_module('ReLU', feature.expand3x3_activation)
        else:
            raise exceptions.NotSupportedException(type(feature))

    # Classifier Layers
    for classify in tv_sqznet1_1.classifier:
        if isinstance(classify, nn.Dropout):
            layers.add_module('Dropout', classify)
        elif isinstance(classify, nn.Conv2d):
            layers.add_module(conv_layer.pop(), classify)
        elif isinstance(classify, nn.ReLU):
            layers.add_module('ReLU', classify)
        elif isinstance(classify, nn.AdaptiveAvgPool2d):
            layers.add_module(pool_layer.pop(), classify)
        else:
            raise exceptions.NotSupportedException(type(classify))

    return layers
Esempio n. 3
0
def alexnet():
    r'''
    Return AlexNet layers which can be supported.
    '''
    conv_layer = ['conv_1', 'conv_2', 'conv_3', 'conv_4', 'conv_5']
    pool_layer = ['pool_1', 'pool_2', 'pool_3']
    fc_layer = ['fc_1', 'fc_2', 'fc_3']

    tv_alexnet = torchvision.models.alexnet(pretrained=False, progress=True)

    tv_alexnet.features[0].padding = (0, 0)
    tv_alexnet.features[0].out_channels = 96
    tv_alexnet.features[3].in_channels = 96
    tv_alexnet.features[3].out_channels = 256
    tv_alexnet.features[6].in_channels = 256
    tv_alexnet.features[8].out_channels = 384
    tv_alexnet.features[10].in_channels = 384

    layers = nn.Sequential()

    conv_layer.reverse()
    pool_layer.reverse()
    fc_layer.reverse()

    # Feature Extraction Layers
    for feature in tv_alexnet.features:
        if isinstance(feature, nn.Conv2d):
            layers.add_module(conv_layer.pop(), feature)
        elif isinstance(feature, nn.ReLU):
            layers.add_module('ReLU', feature)
        elif isinstance(feature, nn.MaxPool2d):
            layers.add_module(pool_layer.pop(), feature)
        else:
            raise exceptions.NotSupportedException(type(feature))

    # Classifier Layers
    for classify in tv_alexnet.classifier:
        if isinstance(classify, nn.Linear):
            layers.add_module(fc_layer.pop(), classify)
        elif isinstance(classify, nn.Dropout):
            layers.add_module('Dropout', classify)
        elif isinstance(classify, nn.ReLU):
            layers.add_module('ReLU', classify)
        else:
            raise exceptions.NotSupportedException(type(classify))

    return layers
Esempio n. 4
0
def vgg19():
    r'''
    Return Vgg19 layers which can be supported.
    '''
    conv_layer = ['conv_1_1', 'conv_1_2',
                  'conv_2_1', 'conv_2_2',
                  'conv_3_1', 'conv_3_2', 'conv_3_3', 'conv_3_4',
                  'conv_4_1', 'conv_4_2', 'conv_4_3', 'conv_4_4',
                  'conv_5_1', 'conv_5_2', 'conv_5_3', 'conv_5_4']
    pool_layer = ['pool_1', 'pool_2', 'pool_3', 'pool_4', 'pool_5']
    fc_layer = ['fc_1', 'fc_2', 'fc_3']

    tv_vgg19 = torchvision.models.vgg19(pretrained=False, progress=True)
    layers = nn.Sequential()

    conv_layer.reverse()
    pool_layer.reverse()
    fc_layer.reverse()

    # Feature Extraction Layers
    for feature in tv_vgg19.features:
        if isinstance(feature, nn.Conv2d):
            layers.add_module(conv_layer.pop(), feature)
        elif isinstance(feature, nn.ReLU):
            layers.add_module('ReLU', feature)
        elif isinstance(feature, nn.MaxPool2d):
            layers.add_module(pool_layer.pop(), feature)
        else:
            raise exceptions.NotSupportedException(type(feature))

    # Classifier Layers
    for classify in tv_vgg19.classifier:
        if isinstance(classify, nn.Linear):
            layers.add_module(fc_layer.pop(), classify)
        elif isinstance(classify, nn.Dropout):
            layers.add_module('Dropout', classify)
        elif isinstance(classify, nn.ReLU):
            layers.add_module('ReLU', classify)
        else:
            raise exceptions.NotSupportedException(type(classify))

    return layers
Esempio n. 5
0
 def read_block(self, connection, block, length, key_a_num=None, key_b_num=None):
     """Either key A or B must be specified for Mifare Classic cards"""
     if not self.card_readable: raise exceptions.NotSupportedException("Read From Card")
     if self.card_authable:
         valid_num = [0x00, 0x01]
         assert key_a_num in valid_num or key_b_num in valid_num
         sector = block >> 2
         if sector >= 32: sector = ((sector-32) >> 2) + 32  # 4K MFC cards have 8 sectors of 16 blocks at the end
         if self.card_authentication != [sector, key_a_num, key_b_num]:
             Reader._auth_mfc(connection, block, key_a_num, key_b_num)
             self.card_authentication = [sector, key_a_num, key_b_num]
     return Reader._read_block(connection, block, length)
Esempio n. 6
0
    def run(self, exe_bin_path='.', presched=False, baseline=0):
        r'''
        Run CNN planner
        '''

        exit_failure = 1
        self.output_width = None
        self.output_height = None

        if not os.path.isdir(self.log_dir):
            print('[Front-end] Make log directory: {}/'.format(self.log_dir))
            os.mkdir(self.log_dir)

        if isinstance(self.module, (nn.Conv2d, nn.Linear)):
            self.output_width = (self.input_width + 2*self.padding_width - self.kernel_width) // self.stride + 1
            self.output_height = (self.input_height + 2*self.padding_height - self.kernel_height)//self.stride + 1
            if self.__run_compiler(exe_bin_path, presched) is exit_failure:
                raise exceptions.CodeGenerationFail
            elif self.__compile_code() is exit_failure:
                raise exceptions.CodeCompileFail
            elif self.__run_binary() is exit_failure:
                raise exceptions.RunCodeFail
            elif self.__run_profiler(exe_bin_path) is exit_failure:
                raise exceptions.ReportFail

        elif isinstance(self.module, nn.MaxPool2d):
            print('[Front-end] MaxPool2d is not supported yet. Just pass.')
            self.output_width = (self.input_width + self.padding_width - self.kernel_width) // self.stride + 1
            self.output_height = (self.input_height + self.padding_height - self.kernel_height) // self.stride + 1
        elif isinstance(self.module, nn.ReLU):
            print('[Front-end] ReLU is not supported yet. Just pass.')
            self.output_width = self.input_width
            self.output_height = self.input_height
        elif isinstance(self.module, nn.Dropout):
            print('[Front-end] Dropout is not supported yet. Just pass.')
            self.output_width = self.input_width
            self.output_height = self.input_height
        elif isinstance(self.module, nn.BatchNorm2d):
            print('[Front-end] BatchNorm2d is not supported yet. Just pass.')
            self.output_width = self.input_width
            self.output_height = self.input_height
        elif isinstance(self.module, nn.AdaptiveAvgPool2d):
            print('[Front-end] AdaptiveAvgPool2d is not supported yet. Just pass.')
            self.output_width = (self.input_width + self.padding_width - self.kernel_width) // self.stride + 1
            self.output_height = (self.input_height + self.padding_height - self.kernel_height) // self.stride + 1
        else:
            raise exceptions.NotSupportedException(type(self.module))

        print('[Front-end] Next layer width/height is {}/{}'.format(self.output_width, self.output_height))

        return torch.Tensor(self.batch, self.output_channel, self.output_height, self.output_width)
Esempio n. 7
0
    def fetch(self):
        # TODO: Make disabling proxy possible
        self._('Getting metadata...', save=False)

        if self._postPayload is not None:
            # do a post request
            ###
            pass
        else:
            # do a get request
            if self._dataFormat.lower() == 'json':
                return yp.get_json(self._endpoint)
            elif self._dataFormat.lower() == 'html':
                return yp.get_html(self._endpoint)
            elif self._dataFormat.lower() == 'xml':
                return yp.get_xml(self._endpoint)
            else:
                raise exceptions.NotSupportedException("Not supported format '{}'!".format(self._dataFormat))
Esempio n. 8
0
 def _transmit(connection, command, command_vars=None):
     """Returns: data as a byte list"""
     if command_vars is None: command_vars = []
     command_desc = command[0]
     full_command = command[1] + command_vars
     sw = [0, 0]
     try:
         data, sw[0], sw[1] = connection.transmit(full_command)
     except (AttributeError, IndexError):
         # Connection lost
         raise exceptions.ConnectionLostException
     response_code = toHexString(sw)
     if response_code == "63 00":
         raise exceptions.FailedException(command_desc)
     elif response_code == "6A 81":
         raise exceptions.NotSupportedException(command_desc)
     elif response_code != "90 00":
         raise exceptions.UnexpectedErrorCodeException(
             response_code, command_desc, sw[0], sw[1])
     return data
Esempio n. 9
0
 def WriteValue(self, value, options):
     print('Default WriteValue called, returning error')
     raise exceptions.NotSupportedException()
Esempio n. 10
0
 def ReadValue(self, options):
     print('Default ReadValue called, returning error')
     raise exceptions.NotSupportedException()
Esempio n. 11
0
 def StopNotify(self):
     print('Default StopNotify called, returning error')
     raise exceptions.NotSupportedException()