Receiving a continuation character Syntax Error - python-3.x

Does anyone know why am I receiving this syntax error?
def softmax(self,x):
return 1/(1+np.exp(-x))
def encrypt(self,pubkey,scaling_factor=1000):
if(not self.encrypted):
self.pubkey = pubkey
self.scaling_factor = float(scaling_factor)
self.encrypted_weights = list()
for weight in model.weights:
self.encrypted_weights.append(self.pubkey.encrypt(\\
int(min(weight,self.maxweight) * self.scaling_factor)))
self.encrypted = True
self.weights = None
return self
File "<ipython-input-33-9ca6863eb9a3>", line 11
self.encrypted_weights.append(self.pubkey.encrypt(\\
^
SyntaxError: unexpected character after line continuation character

Well, I think you forgot to put the keyword self before model.weight, and import np from numpy. Try this:
import np
def softmax(self,x):
return 1/(1+np.exp(-x))
def encrypt(self,pubkey,scaling_factor=1000):
if(not self.encrypted):
self.pubkey = pubkey
self.scaling_factor = float(scaling_factor)
self.encrypted_weights = list()
for weight in self.model.weights:
self.encrypted_weights.append(self.pubkey.encrypt(int(min(weight, self.maxweight) * self.scaling_factor)))
self.encrypted = True
self.weights = None
return self

Related

Name error in class when importing my script file to another from

I wrote some code in a script file script.py
from functools import reduce
class Operators:
def __init__(self, names):
self.op_name = []
for name in names:
self.op_name.append(name)
def __matmul__(self, other):
if type(other) is Operators:
new_op_list = self.op_name + other.op_name
return Operators(new_op_list)
def __str__(self):
return ' # '.join(map(str, self.op_name))
class Single_Ket:
def __init__(self, label: tuple, coeff, operator = []):
self.label = label
self.coeff = coeff
self.operators = operator
def __str__(self):
Operator_Str = str( eval(' # '.join(map(str, self.operators))) )
if type(self.label) is tuple:
return '{} * {} # | {} >'.format(self.coeff, Operator_Str, ', '.join(map(str, self.label)))
else:
return f'{self.coeff} * {Operator_Str}.|{self.label}>'
if __name__ == '__main__':
Jp = Operators(['Jp'])
Jm = Operators(['Jm'])
ket_1 = Single_Ket((1, 1), 1, [Jp # Jm])
print(ket_1)
which works fine when I run it.
However, when I import test.py into another file test.py and run
from script import*
Jp = Operators(['Jp'])
Jm = Operators(['Jm'])
ket_1 = Single_Ket((1, 1), 1, [Jp # Jm])
print(ket_1)
I got the error message 'NameError: name 'Jp' is not defined'. Given I defined Jp in test.py, I don't understand the reason why I get name error.
What is the way to fix this, or is there any related article I can look at to fix this?

How do you parse the bin file from INT8 Calibration of TensorRT?

I have created a python script for calibrating(INT8) the dynamic scales of the activation of TinyYOLO V2 using TensorRT. The script gave me a file called calibration_cache.bin. How do I parse the .bin file ? What do the values inside the .bin file mean ?
calibrator.py
import pycuda.driver as cuda
import pycuda.autoinit
import numpy as np
from PIL import Image
import ctypes
import tensorrt as trt
import os
CHANNEL = 3
HEIGHT = 416
WIDTH = 416
class PythonEntropyCalibrator(trt.IInt8EntropyCalibrator):
def __init__(self, input_layers, stream):
trt.IInt8EntropyCalibrator.__init__(self)
self.input_layers = input_layers
self.stream = stream
self.d_input = cuda.mem_alloc(self.stream.calibration_data.nbytes)
stream.reset()
def get_batch_size(self):
return self.stream.batch_size
def get_batch(self, bindings, names):
batch = self.stream.next_batch()
if not batch.size:
return None
cuda.memcpy_htod(self.d_input, batch)
for i in self.input_layers[0]:
assert names[0] != i
bindings[0] = int(self.d_input)
return bindings
def read_calibration_cache(self, length=0):
if os.path.exists('calibration_cache.bin'):
with open('calibration_cache.bin', 'rb') as f:
return f.read()
return None
def write_calibration_cache(self, cache, size=0):
with open('calibration_cache.bin', 'wb') as f:
f.write(cache)
return None
class ImageBatchStream():
def __init__(self, batch_size, calibration_files, preprocessor):
self.batch_size = batch_size
self.max_batches = (len(calibration_files) // batch_size) + \
(1 if (len(calibration_files) % batch_size) \
else 0)
self.files = calibration_files
self.calibration_data = np.zeros((batch_size, CHANNEL, HEIGHT, WIDTH), \
dtype=np.float32)
self.batch = 0
self.preprocessor = preprocessor
#staticmethod
def read_image_chw(path):
img = Image.open(path).resize((WIDTH,HEIGHT), Image.NEAREST)
im = np.array(img, dtype=np.float32, order='C')
im = im[:,:,::-1]
im = im.transpose((2,0,1))
return im
def reset(self):
self.batch = 0
def next_batch(self):
if self.batch < self.max_batches:
imgs = []
files_for_batch = self.files[self.batch_size * self.batch : \
self.batch_size * (self.batch + 1)]
for f in files_for_batch:
self.batch_size * (self.batch + 1)]
for f in files_for_batch:
print("[ImageBatchStream] Processing ", f)
img = ImageBatchStream.read_image_chw(f)
img = self.preprocessor(img)
imgs.append(img)
for i in range(len(imgs)):
self.calibration_data[i] = imgs[i]
self.batch += 1
return np.ascontiguousarray(self.calibration_data, dtype=np.float32)
else:
return np.array([])
test.py
from random import shuffle
from PIL import Image
import glob
import numpy as np
import tensorrt as trt
import pycuda.driver as cuda
import pycuda.autoinit
import os
from calibrator import *
TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
model_file = './tiny_yolov2/Model.onnx'
dataset_loc = './Dataset/*.jpg'
def normalize(data):
data /= 255.0
return data
def create_calibration_dataset():
calibration_files = glob.glob(dataset_loc)
shuffle(calibration_files)
return calibration_files[:20]
calibration_files = create_calibration_dataset()
NUM_IMAGES_PER_BATCH = 5
batchstream = ImageBatchStream(NUM_IMAGES_PER_BATCH, calibration_files, normalize)
Int8_calibrator = PythonEntropyCalibrator(["conv2d_91_input"], batchstream)
builder = trt.Builder(TRT_LOGGER)
builder.int8_calibrator = Int8_calibrator
builder.refittable = True
builder.int8_mode = True
network = builder.create_network()
parser = trt.OnnxParser(network, TRT_LOGGER)
print(builder.int8_mode, builder.platform_has_fast_int8,builder.refittable)
with open(model_file, 'rb') as model:
parser.parse(model.read())
print('Done reading ONNX File\n')
engine = builder.build_cuda_engine(network)
print(engine, TRT_LOGGER)
with open("model.trt", "wb") as f:
f.write(engine.serialize())
print("Done converting the ONNX to TRT\n")
tinyolo_fitter = trt.Refitter(engine, TRT_LOGGER)
print(tinyolo_fitter.refit_cuda_engine())
print(tinyolo_fitter.get_tensors_with_dynamic_range())
calibration_cache.bin
TRT-5105-EntropyCalibration
image: 3c010a14
scalerPreprocessor_scaled: 38018ba0
image2: 38018ba0
batchnormalization_1_output: 3d07b31d
leakyrelu_1_output: 3c98a317
maxpooling2d_1_output: 3c1e5b30
batchnormalization_2_output: 3ca6aa67
leakyrelu_2_output: 3ca6aa67
maxpooling2d_2_output: 3c82cf7d
batchnormalization_3_output: 3ce07ce8
leakyrelu_3_output: 3ce52236
maxpooling2d_3_output: 3cc8ed6f
batchnormalization_4_output: 3d3df55f
leakyrelu_4_output: 3c651727
maxpooling2d_4_output: 3cec84fc
batchnormalization_5_output: 3d0f51e3
leakyrelu_5_output: 3cb52377
maxpooling2d_5_output: 3d026049
batchnormalization_6_output: 3d387291
leakyrelu_6_output: 3ccc009a
maxpooling2d_6_output: 3c8d0f0c
batchnormalization_7_output: 3e0de3d2
leakyrelu_7_output: 3d7b4397
batchnormalization_8_output: 3cc459d6
leakyrelu_8_output: 3cbd9562
grid: 3ddc32dc
def read_calibration_cache(self, length=0):
if os.path.exists('calibration_cache.bin'):
with open('calibration_cache.bin', 'rb') as f:
return f.read()
return None
This does the work i guess. If there is a calibration_cache.bin file in your dir, calibrator parses it instead of calibrating again.

Namerror in python for self in the for loop ,

class Support_Vector_Machine():
def __init__(self, visualization=True):
self.visualization = visualization
self.colors = {1:'r',-1:'b'}
if self.visualization:
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1,1,1)
# train
def fit(self, data):
self.data=data
opt_dict = {}
transforms = [[1,1],
[-1,-1],[1,-1],[-1,1 ]]
all_data=[]
for yi in **self.data :**
for featureset in self.data[yi]:
for feature in featureset:
self.max_feature_value = max(all_data)
self.min_feature_value = min(all_data)
all_data= None
In the above code , I am trying to loop a variable yi through self.data which throws the name error.
Help me resolve the error.
it says:
line 21, in Support_Vector_Machine
for yi in self.data :
NameError: name 'self' is not defined
In Python, indentation matters. The whole for block is misindented:
class Support_Vector_Machine():
def __init__(self, visualization=True):
self.visualization = visualization
self.colors = {1:'r',-1:'b'}
if self.visualization:
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1,1,1)
# train
def fit(self, data):
self.data=data
opt_dict = {}
transforms = [[1,1],
[-1,-1],[1,-1],[-1,1 ]]
all_data=[]
for yi in self.data:
for featureset in self.data[yi]:
for feature in featureset:
self.max_feature_value = max(all_data)
self.min_feature_value = min(all_data)
all_data= None

NameError while calling module, declaring default values in a class function

I'm trying to understand Public Key encryption so I wrote this little module using PyCryptodome and the RSA/PKCS1_OAEP module on Python 3 to help me along. However, I keep getting an error:
NameError: name 'aesenc' is not defined
This is a two part question:
In standalone code (outside a class) the arg = default_val code will work, but I'm pretty sure this code will throw an error (assuming I fix question #2). I also know I can't use self.default_val as that needs an object to be created first. How do I assign a default value (in this case, the private/public key of the object?)
With regard to the error message, a vgrep reveals that the suite has been declared before its been called but I still get the NameError. Can someone please take a look and let me know what I'm doing wrong?
Modules: (Breaking into parts as SO keeps jumbling the code)
from passlib.context import CryptContext
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Random import get_random_bytes
The Class:
class statEnc:
pubcipher = None
privcipher = None
pubkeystr = None
privkeystr = None
sessionkey = None
def __init__(self, pubkeystr = None, privkeystr = None, sessionkey = None):
self.pubkeystr = pubkeystr
self.privkeystr = privkeystr
self.sessionkey = sessionkey
if pubkeystr == None or privkeystr == None: #if blank, generate keys
self.random_generator = Random.new().read
self.keys = RSA.generate(1024, self.random_generator)
self.pubkey = self.keys.publickey()
self.pubkeystr = self.pubkey.exportKey(format='PEM',
passphrase=None,
pkcs=1)
self.pubcipher = PKCS1_OAEP.new(self.pubkey)
self.privcipher = PKCS1_OAEP.new(self.keys)
self.privkeystr = self.keys.exportKey(format='PEM',
passphrase=None,
pkcs=1)
self.privkey = self.keys.exportKey()
else: #import the keys
self.pubkey = RSA.importKey(pubkeystr)
self.pubcipher = PKCS1_OAEP.new(pubkey)
self.privkey = RSA.importKey(privkeystr)
self.pubcipher = PKCS1_OAEP.new(privkey)
if sessionkey == None:
sessionkey = get_random_bytes(16)
else:
self.sessionkey = sessionkey
def encrypt_val(self, session_key, cipher = pubcipher):
# a little ditty to hep encrypt the AES session key
try:
session_key = session_key.encode('utf8')
except:
pass
ciphertext = cipher.encrypt(session_key)
return ciphertext
def decrypt_val(self, ciphertext, cipher = privcipher):
# a little ditty to hep decrypt the AES session key
session_key = cipher.decrypt(ciphertext)
try:
session_key = session_key.decode('utf8')
except:
pass
return session_key
def aesenc(self, data, *args):
#encrypt the payload using AES
key = ''
if args:
if 'str' in str(type(args[0])):
try:
key = int(args[0])
except:
key = get_random_bytes(16)
else:
key = get_random_bytes(16)
try:
data = data.encode('utf8')
except:
pass
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
aesencdict = {
'ciphertext' : ciphertext,
'tag' : tag,
'nonce' : cipher.nonce ,
'key' : key
}
return(aesencdict)
def aesdec(self, aesdict):
#decrypt the AES encrypted payload
cipher = AES.new(aesdict['key'], AES.MODE_EAX, aesdict['nonce'])
data = cipher.decrypt_and_verify(aesdict['ciphertext'], aesdict['tag'])
try:
data = data.decode('utf8')
except:
pass
return data
def end2enc(self, val, cipher = pubcipher):
# a master function to first encrypt the payload
val = str(val)
encval = aesenc(val)
# and then PK encrypt the key
encval['key'] = encrypt_val(encval['key'], cipher)
return encval
def end2dec(self, encval, cipher = privcipher):
encval['key'] = decrypt_val(encval['key'], cipher)
outval = aesdec(encval['aesdict'], encval['key'])
return outval
Test function and main:
def test():
val = { 'test' : "hello"}
keypair = statEnc()
print(str(type(keypair)))
encval = keypair.end2enc(val, keypair.pubcipher)
outval = keypair.end2dec(encval, keypair.privcipher)
print(val, outval)
if val == eval(outval):
return(val)
else:
return False
if __name__ == '__main__':
test()
[UPDATE]
The Traceback is as follows:
[guha#katana stat]$ python statenc.py
<class '__main__.statEnc'>
Traceback (most recent call last):
File "statenc.py", line 124, in <module>
test()
File "statenc.py", line 115, in test
encval = keypair.end2enc(val, keypair.pubcipher)
File "statenc.py", line 100, in end2enc
encval = aesenc(val)
NameError: name 'aesenc' is not defined
Slept over my question and woke up with a fresh mind, and voila! the answer presented itself.
The answer to the second question is as follows:
2. putting a simple 'self.' solves the problem - i was calling 'aesenc(params)' instead of 'self.aesenc(params)'. Stupid of me, really.
Question 1 is answered in this SO question.

Tkinter Text widget maximum recursion depth using idlelib Delegator/Percolator

After testing I have concluded that the error
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
text.mainloop()
File "C:\Program Files\Python32\lib\tkinter\__init__.py", line 1009, \
in mainloop
self.tk.mainloop(n)
RuntimeError: maximum recursion depth exceeded
occurs only after there is a character before the INSERT position and the user types a Backspace. Here is my code:
# Import
import tkinter
import tkinter.scrolledtext
import idlelib.Delegator
import idlelib.WidgetRedirector
import idlelib.Percolator
import idlelib.ColorDelegator
import time
import re
# Class
class Highlighter(idlelib.Delegator.Delegator):
def __init__(self):
idlelib.Delegator.Delegator.__init__(self)
self._syntaxes = {}
self._compiled = ""
def setdelegate(self, delegate):
idlelib.Delegator.Delegator.setdelegate(self, delegate)
if delegate is not None:
self.setup()
def insert(self, index, chars, *args):
index = self.index(index)
self.delegate.insert(index, chars, *args)
self.update(index, index + "+%dc" % len(chars))
def delete(self, index1, index2=None):
index = self.index(index1)
self.delete(index1, index2)
self.update(index)
def update(self, index1, index2=None):
self.tag_add("TODO", index1, index2)
self.after(1, self.syntax_highlight)
def setup(self):
background = self.cget("background")
self.tag_configure("TODO", foreground=background, background=None)
def syntax_configure(self, tag, syntax):
"""Configure syntax TAG with pattern SYNTAX."""
self._syntaxes[tag] = syntax
syntax_config = syntax_configure
def syntax_cget(self, tag):
"""Return the pattern of TAG."""
return self._syntaxes[tag]
def syntax_delete(self, tag):
"""Delete syntax TAG from the highlighter."""
self._syntaxes.pop(tag)
def syntax_clear(self):
"""Clear all syntaxes."""
self._syntaxes.clear()
def syntax_compile(self):
"""Compile all syntaxes."""
return "|".join("(?P<%s>%s)" % (tag, pattern)
for tag, pattern in self._syntaxes.items())
def syntax_highlight(self, start="1.0", end="end"):
start = self.index(start)
end = self.index(end)
pattern = self.syntax_compile()
if pattern:
for match in re.finditer(pattern, self.get(start, end)):
tag, sequence = list(match.groupdict().items())[0]
start, stop = match.span()
self.tag_remove("TODO", "1.0", "1.0+%dc" % match.span()[1])
self.tag_add(tag, "1.0+%dc" % start, "1.0+%dc" % stop)
self.tag_delete("TODO")
text = tkinter.Text()
text.pack()
p = idlelib.Percolator.Percolator(text)
h = Highlighter()
p.insertfilter(h)
text.mainloop()
If anyone could explain what the cause/source of the recursion is, it would be much appreciated. Thanks!
The issue was that instead of self.delete(index1, index2) in delete it should have been self.delegate.delete(index1, index2).

Resources