def __init__(self, vec_dim, dim, options={}): """ Instantiate the Layer (which will later be used to connect tensors) Parameters ---------- vec_dim : int (width of the vector generated by the vectorizer) dim : int (width of vectors in the rest of the network) """ super(StackedBiLSTM, self).__init__() # Embedder (mostly to upscale the incoming character vectors) self.embedder = Embedder(vec_dim, dim) # Left to right Stacked LSTM self.left2rightA = LSTMCell(dim, options=options) # Outputs two length-dim vectors self.left2rightB = LSTMCell(dim, options=options) # Outputs two length-dim vectors # Right to left Stacked LSTM self.right2leftA = LSTMCell(dim, options=options) # Outputs two length-dim vectors self.right2leftB = LSTMCell(dim, options=options) # Outputs two length-dim vectors # Output decision layer self.splitLayer = SplitLayer(dim, options=options) # Outputs a one-dimensional length-2 vector self.Lzeros = pu.var_zeros(dim, ttype=options.get('ttype')) # Zeros for initialization from left self.Lhzeros = pu.var_zeros(dim, ttype=options.get('ttype')) # Zeros for initialization (hidden layer) self.Rzeros = pu.var_zeros(dim, ttype=options.get('ttype')) # same, for the right self.Rhzeros = pu.var_zeros(dim, ttype=options.get('ttype')) self.training = False # By default, but set to True during training with 'train()'
def calc_embeddings(docs: List[str], batch_size: int, root: str) -> np.ndarray: """Calculate embeddings (in batches). Args: docs: List of documents filenames. batch_size: Batch size. root: Root directory. Returns: Numpy array of (N, 768) of texts embeddings. """ embedder = Embedder() all_embeddings = np.zeros((len(docs), 768), dtype=np.float32) iters = len(docs) // batch_size if len(docs) % batch_size > 0: iters += 1 for i in trange(iters): batch = docs[i * batch_size:(i + 1) * batch_size] filenames = [os.path.join(root, doc) for doc in batch] texts = [get_text_reduced(x, maxlen=512) for x in filenames] embeddings = embedder.embed(texts) all_embeddings[i * batch_size:(i + 1) * batch_size] = embeddings return all_embeddings
def main(): args = arg_parse() docs = [ os.path.join(dir, f) for dir in os.listdir(args.root) for f in os.listdir(os.path.join(args.root, dir)) ] docs = sorted(docs) index = Indexer(docs, args.index, args.root) embedder = Embedder() # L0 q_pos, q_neg = query_expand(args.query) # Get all OR-ed tokens q_pos_expand = re.sub(r" ", " AND ", q_pos) hits = index.query_boolean(q_pos_expand.split()) # Remove all NOT-ed tokens if q_neg: for token in q_neg.split(): term = index.stemmer.stem(token) try: not_posting = index.tfidf(index.index[term]) except KeyError: not_posting = [] hits = not_and_postings(not_posting, hits) if not hits: print("nothing found") return hits = sorted(hits, key=lambda item: item[1], reverse=True) hits = hits[:args.l0_size] # L1 doc_ids = [x[0] for x in hits] filenames = [os.path.join(args.root, docs[i]) for i in doc_ids] texts = [get_text_reduced(x, maxlen=512) for x in filenames] if args.batch_size >= args.l0_size: embeddings = embedder.embed(texts) else: embeddings = batch_embed(embedder, texts, args.batch_size) query_emb = embedder.embed([q_pos])[0] dist_cos = [cosine(query_emb, e) for e in embeddings] idx_cos = np.argsort(dist_cos) # Render q_red = query_reduce(args.query) resorted = [doc_ids[i] for i in idx_cos] for i, id in enumerate(resorted[:args.l1_size]): print("\n{}:".format(i)) index.render_file(q_red.split(), docs[id]) orig_pos = idx_cos[i] print("\tL0 rank = {}; tf-idf = {:.3f}; cos-sim = {:.3f}".format( orig_pos, hits[orig_pos][1], 1 - dist_cos[orig_pos]))
def build_model(config, src_field, tgt_field): embed_dim = config.embed_dim hidden_dim = config.hidden_dim num_layers = config.num_layers dropout_p = config.dropout_p attention_type = config.attention beam_width = config.beam_width model = Seq2Seq( LstmEncoder(Embedder(src_field, embed_dim, dropout_p), hidden_dim, num_layers, dropout_p), LstmDecoder(Embedder(tgt_field, embed_dim, dropout_p), hidden_dim, num_layers, dropout_p, build_attention(hidden_dim, attention_type), beam_width), ) return model
def __init__(self, env, loss_fn, optim, gym=gym, preprocess_func=None, utility_func=None, embed=False, flatten=False, frameskip=1, batch_size=32, cuda=False, ale=False, init_skip=0, mid_skip=0, caution=0.0, render_skip=1): self.n_agents = 1 if utility_func == None: raise Exception("Need utility function") if preprocess_func == None: raise Exception("Need preprocess function") if self.n_agents < 1: raise Exception("Need at least 1 agent.") if self.n_agents > 1: raise NotImplementedError super(Coach, self).__init__() print("Initializing Coach") self.ale = ale self.name = env self.gym = gym self.dones = [False for _ in range(self.n_agents)] self.length = 0 self.memory = Memory(self.n_agents) self.episode = [] self.embedder = Embedder() self.embedding = embed self.flatten = flatten self.best_episode = [] self.p_obs = torch.zeros(1) self.values = [] self.frameskip = frameskip self.loss_fn=loss_fn self.optim = optim self.preview = False self.single_step = False self.norm = True self.render = False self.debug_render = False self.caution = caution#0.95 self.life = -1 self.life = -1 self.init_skip = init_skip#70 self.render_frameskip = render_skip self.mid_skip = mid_skip#50 self.steps = 0 self.batch_size = batch_size self.preprocess_frame = preprocess_func self.update = utility_func self.cuda = cuda atexit.register(self.end) #create environments self.envs = [self.gym.make(self.name) for _ in range(self.n_agents)] self.env = 0 self.actions = self.envs[0].action_space.n os.system("toilet {}".format(self.name))
def build_model(cfg, char_voca, word_voca=None, gazet=None, pos_voca=None): """Build Neural Network based Ner model (Embedder + Classifier)""" # Build Embedder embedder = Embedder(window=cfg.window, char_voca=char_voca, word_voca=word_voca, jaso_dim=cfg.jaso_dim, char_dim=cfg.char_dim, word_dim=cfg.word_dim, gazet=gazet, gazet_embed=True, pos_enc=True, phoneme=True, pos_voca_size=len(pos_voca), pos_dim=cfg.pos_dim) print('Total Embedding_size: ', embedder.embed_dim) encoder_name, decoder_name = cfg.model_name.lower().split('-') # Build Encoder if encoder_name == 'fnn5': encoder = models.Fnn5(context_len=cfg.context_len, in_dim=embedder.embed_dim, hidden_dim=cfg.hidden_dim) elif encoder_name == 'cnn7': encoder = models.Cnn7(in_dim=embedder.embed_dim, hidden_dim=cfg.hidden_dim) elif encoder_name == 'cnn8': encoder = models.Cnn8(context_len=cfg.context_len, in_dim=embedder.embed_dim, hidden_dim=cfg.hidden_dim) elif encoder_name in ['gru', 'lstm', 'sru']: encoder = models.RnnEncoder(context_len=cfg.context_len, in_dim=embedder.embed_dim, out_dim=cfg.hidden_dim, cell=encoder_name) else: raise ValueError('unknown model name: %s' % cfg.model_name) # Build Decoder if decoder_name.lower() == 'fc': decoder = models.FCDecoder(in_dim=encoder.out_dim, hidden_dim=cfg.hidden_dim, n_tags=cfg.n_tags) elif decoder_name in ['gru', 'lstm', 'sru']: decoder = models.RnnDecoder(in_dim=encoder.out_dim, hidden_dim=cfg.hidden_dim, n_tags=cfg.n_tags, num_layers=cfg.num_layers, cell=decoder_name) model = models.Ner(embedder, encoder, decoder) return model
def __init__(self, trained_model_PATH, vocabulary='./XX/vocabulary.pkl', tag='./XX/tags.pkl', status='raw'): self.trained_model_PATH = trained_model_PATH self.vocabulary = vocabulary self.tag = tag self.model = None self.embedding = Embedder(vocabulary, tag) self.loadmodel()
def embed(stego, secret, chromosome): """Embed secret message into the host using the chromosome""" if len(chromosome) > 7: chromosome = helper_individual.packchromosome(chromosome) # Convert to a flattened pixel sequence stego_sequence = MatScanner.scan_genetic(stego, chromosome) secret = secret.flatten() stego_sequence = Embedder.embed(stego_sequence, secret, chromosome) # Reshape the stego image return MatScanner.reshape_genetic(stego_sequence, stego.shape, chromosome)
class Application: def __init__(self): self.messenger = Messenger() self.embedder = Embedder() def start(self): text = self.messenger.get_text() if text is not None: lang = detect(text) if lang == "en" or lang == "ne": sentences, embeddings = self.embedder.encode(text, lang) self.messenger.send_embeddings(sentences, embeddings, lang)
def main(): embedder = Embedder() parser = DocParser() # iterate through grobid with open('grobid_data.pkl', 'wb') as output: for subdir, dirs, files in os.walk(grobid_path): print(len(files)) count = 0 for file in files: print(count) count += 1 # print(os.path.join(subdir, file)) doc = parser.parseXML(os.path.join(subdir, file)) doc.id = str(file).split('.')[0] if len(doc.abstract) == 0: continue doc.embedding = embedder.embed(doc.abstract) # pair = variablesFromPair((doc.abstract, doc.title), word_index, embedding_map) # if (len(pair[0]) == 0 or len(pair[1]) == 0): # continue # doc.embedding = encode(encoder, pair[0]) pickle.dump(doc, output, pickle.HIGHEST_PROTOCOL)
def __init__(self, vec_dim, dim, options={}): """ Instantiate the object and set options """ print("options:", options) self.set_options(options, default) options['ttype'] = self.get('ttype') self.validation_graph = None # self.bilstm = StackedBiLSTM(vec_dim, dim, options=options) # A Bi-directional LSTM self.bilstm = SimpleBiLSTM(dim, options=options) # A Bi-directional LSTM self.embedder = Embedder( vec_dim, dim, options=options ) # Embedder (mostly for upscaling incoming vectors) self.class_weights = pu.torchvar(self.get('class_weights'), ttype=self.get('ttype')) # Loss functions self.coef1 = pu.torchvar([0.1]) self.coef2 = pu.torchvar([0.2]) self.coef3 = pu.torchvar([0.3]) self.coef4 = pu.torchvar([0.4]) self.accuracyLoss = pu.AccuracyLoss() self.skewLoss = pu.SkewedL1Loss(self.class_weights) self.criterion_CE = nn.CrossEntropyLoss(weight=self.class_weights, size_average=False) self.criterion_MSE = nn.MSELoss() self.orig_loss = None if torch.cuda.is_available(): self.bilstm = self.bilstm.cuda() self.coef1 = self.coef1.cuda() self.coef2 = self.coef2.cuda() self.coef3 = self.coef3.cuda() self.coef4 = self.coef4.cuda() self.accuracyLoss = self.accuracyLoss.cuda() self.skewLoss = self.skewLoss.cuda() self.criterion_CE = self.criterion_CE.cuda() self.criterion_MSE = self.criterion_MSE.cuda() self.eval_mode() # eval mode by default
def batch_embed(embedder: Embedder, texts: List[str], batch_size: int) -> np.ndarray: """Get embeddings in batches if GPU memory is not enough. Args: embedder: Embedder with DistilBERT model. texts: List of songs' texts. batch_size: Batch size. Returns: Numpy array of (N, 768) of texts embeddings. """ embeddings = np.zeros((len(texts), 768)) iters = len(texts) // batch_size if len(texts) % batch_size > 0: iters += 1 for i in range(iters): batch = texts[i * batch_size:(i + 1) * batch_size] emb_batch = embedder.embed(batch) embeddings[i * batch_size:(i + 1) * batch_size] = emb_batch return embeddings
def __init__(self, cfg): super(GenericModel, self).__init__() self.cfg = cfg self.embedder = Embedder(cfg['embedder']) self.reasoner = Reasoner(cfg['reasoner'])
# epochs for METN pre training if args.property is 'QED': embedder_epochs_num = 1 elif args.property is 'DRD2': embedder_epochs_num = 12 else: print("property must bt 'QED 'or 'DRD2'") exit() if args.conditional or args.no_pre_train: embedder_epochs_num = 0 if args.is_CDN is True: _, _, boundaries = create_dataset(args.property, rebuild_dataset=False) dataset_CDN = Dataset('dataset/' + args.property + '/CDN/CDN') model_CDN = Embedder(dataset_CDN, 'CDN', args).to(device) embedder_epochs_num = args.epochs fit(args, model_CDN, embedder_epochs_num, boundaries, is_validation=True) exit() # prepare dataset dataset_file_A, dataset_file_B, boundaries = create_dataset(args.property, args.rebuild_dataset) dataset_A = Dataset(dataset_file_A, use_atom_tokenizer=args.tokenize, isB=False) dataset_B = Dataset(dataset_file_B, use_atom_tokenizer=args.tokenize, isB=True) # create and pre-train the embedders (METNs) model_A = Embedder(dataset_A, 'Embedder A', args).to(device) fit(args, model_A, embedder_epochs_num, boundaries, is_validation=True) model_B = Embedder(dataset_B, 'Embedder B', args).to(device) fit(args, model_B, embedder_epochs_num, boundaries, is_validation=False)
from detectors import DetectorSSD, DetectorVJ, DetectorHOG, DetectorMMOD, DetectorLBP from embedder import Embedder from recognizer import Recognizer import recognize_process as rp detector_vj = DetectorVJ() detector_ssd = DetectorSSD() detector_hog = DetectorHOG() detector_mmod = DetectorMMOD() detector_lbp = DetectorLBP() embedder = Embedder() recognizer = Recognizer() # вычисление областей ROI всеми детекторами на датасете def calc_all_detectors(): print('Haar detector calculating...') detector_vj.calc_dataset() print('SSD detector calculating...') detector_ssd.calc_dataset() print('HOG detector calculating...') detector_hog.calc_dataset() print('MMOD detector calculating...') detector_mmod.calc_dataset() print('LBP detector calculating...') detector_lbp.calc_dataset()
for part in parts: if len(part.split()) > 1: labels.append(part.split()[0] + ''.join(''.join([w[0].upper(), w[1:].lower()]) for w in part.split()[1:])) if useSynonyms: predicates = [max(part.split(), key=len) for part in parts] if predicates is not None and len(predicates) > 0: for predicate in predicates: for part in list(parts): if predicate in part: for syn in gloveModel.gloveModel.most_similar( predicate.lower()): parts.append(part.replace(predicate, syn[0])) if len(parts) == 0: resultsExists = False parts = list(splitter.split(gen_question, min=minLen, max=maxLen)) # create embedder part vectors = [] for part in parts: vectors.append(gloveModel.getVector(part)) return vectors, parts, pos, gen_question, labels, resultsExists # ===== main testing ===== if __name__ == "__main__": pp( processQuestion(Embedder('../glove.6B.50d.txt'), 'who was named as president of the USA', useAPI=True, useSynonyms=True))
def __init__(self): self.messenger = Messenger() self.embedder = Embedder()
class StackedBiLSTM(nn.Module): """ Bi-directional Stacked LSTM, designed to read a whole line and make predictions on each interstitial. The stacking is done simply by using two LSTMs where there was previously one. They feed into each other before producing output. For a line containing N characters, N-1 predictions will be made. This layer also commands the "underlying" layers: left2right (A and B), right2left, splitLayer. This layer takes care of setting modes, and saving / loading. """ def __init__(self, vec_dim, dim, options={}): """ Instantiate the Layer (which will later be used to connect tensors) Parameters ---------- vec_dim : int (width of the vector generated by the vectorizer) dim : int (width of vectors in the rest of the network) """ super(StackedBiLSTM, self).__init__() # Embedder (mostly to upscale the incoming character vectors) self.embedder = Embedder(vec_dim, dim) # Left to right Stacked LSTM self.left2rightA = LSTMCell(dim, options=options) # Outputs two length-dim vectors self.left2rightB = LSTMCell(dim, options=options) # Outputs two length-dim vectors # Right to left Stacked LSTM self.right2leftA = LSTMCell(dim, options=options) # Outputs two length-dim vectors self.right2leftB = LSTMCell(dim, options=options) # Outputs two length-dim vectors # Output decision layer self.splitLayer = SplitLayer(dim, options=options) # Outputs a one-dimensional length-2 vector self.Lzeros = pu.var_zeros(dim, ttype=options.get('ttype')) # Zeros for initialization from left self.Lhzeros = pu.var_zeros(dim, ttype=options.get('ttype')) # Zeros for initialization (hidden layer) self.Rzeros = pu.var_zeros(dim, ttype=options.get('ttype')) # same, for the right self.Rhzeros = pu.var_zeros(dim, ttype=options.get('ttype')) self.training = False # By default, but set to True during training with 'train()' def get_parameters(self): """ Return a list of trainable parameters for the PyTorch optimizer """ parameters = list(self.parameters()) parameters.extend( list( self.embedder.parameters() )) parameters.extend( list( self.left2rightA.parameters() )) parameters.extend( list( self.left2rightB.parameters() )) parameters.extend( list( self.right2leftA.parameters() )) parameters.extend( list( self.right2leftB.parameters() )) parameters.extend( list( self.splitLayer.parameters() )) return parameters def training_mode(self): """ Set models for training mode """ self.train() self.embedder.train() self.left2rightA.train() self.left2rightB.train() self.right2leftA.train() self.right2leftB.train() self.splitLayer.train() def eval_mode(self): """ Set models for evaluation mode, which has, for instance, no dropout """ self.eval() self.embedder.eval() self.left2rightA.eval() self.left2rightB.eval() self.right2leftA.eval() self.right2leftB.eval() self.splitLayer.eval() def save(self, dirpath): """ Save the current state of the model """ try: torch.save(self.state_dict(), dirpath+'/StackedBiLSTM.pth') except: raise self.embedder.save(dirpath) self.left2rightA.save(dirpath, '/left2rightA.pth') self.left2rightB.save(dirpath, '/left2rightB.pth') self.right2leftA.save(dirpath, '/right2leftA.pth') self.right2leftB.save(dirpath, '/right2leftB.pth') self.splitLayer.save(dirpath) def load(self, dirpath): """ Load the models from a specified directory. Toss exceptions because models won't exist on the first run. """ try: state_dict = torch.load(dirpath+'/StackedBiLSTM.pth', map_location=lambda storage, loc: storage) except: pass self.embedder.load(dirpath) self.left2rightA.load(dirpath, '/left2rightA.pth') self.left2rightB.load(dirpath, '/left2rightB.pth') self.right2leftA.load(dirpath, '/right2leftA.pth') self.right2leftB.load(dirpath, '/right2leftB.pth') self.splitLayer.load(dirpath) def forward(self, line): """ The 'forward()' for this network. This function takes one sample at a time, where each sample is a line of text. Each LSTM, as it proceeds character by character, it makes a prediction *after* having consumed the character on both sides of an interstice. Parameters ---------- line : (tensor) list of N vectors (where each vector represents one character) Returns ------- (tensor) list of N-1 pairs (each pair of floats represents one interstice-- both the probability of no split and yes split) """ inputs = [] # List of list of tensor. Will hold the cell and hidden states at each possible split location # i.e. the value associated with the first interstice will be at index 0, etc. ### Run line through the left2right LSTM ### LCA = self.Lzeros # left2right cell state LHA = self.Lhzeros # left2right hidden state LCB = self.Lzeros # same for right2left ... LHB = self.Lhzeros num = 0 loc = 0 # First interstice is number 0 for x in line: x = self.embedder(x) # Get the states for interstice number 'loc' LCA, LHA = self.left2rightA(x, LCA, LHA) # Run this character through the Left->-Right LSTM LCB, LHB = self.left2rightB(LHA, LCB, LHB) # Run this character through the second Left->-Right LSTM # If we have consumed at least two characters, we will store our first output, which applies to the interstice between the last two characters if num >= 1: inputs.append( [LCB, LHB] ) assert(inputs[loc] == [LCB, LHB]) assert(len(inputs[loc]) == 2) loc += 1 # For instance, loc=0 represents interstitial between the first and second char num += 1 ### Run line through the right2left LSTM ### RCA = self.Rzeros RHA = self.Rhzeros RCB = self.Rzeros RHB = self.Rhzeros num = 0 for x in reversed(line): # Iterate backwards through the same list of vectors x = self.embedder(x) # Get the states for interstice number 'loc' RCA, RHA = self.right2leftA(x, RCA, RHA) # Run this character through the Right->-Left LSTM RCB, RHB = self.right2leftB(RHA, RCB, RHB) # Run this character through the second Right->-Left LSTM if num >= 1: loc -= 1 # Keep this process in reverse alignment with the other try: inputs[loc].extend([RCB, RHB]) assert(len(inputs[loc]) == 4) except: raise num += 1 # Just to keep track of how many characters we've consumed ### Combine output from both LSTMs and run through the SplitLayer ### # 'preds' will have one fewer element than 'line' because each prediction applies to the interstitial between two elements of 'line' preds = None for item in inputs: LC, LH, RC, RH = item # Just the output from the second layer of each stacking pred = self.splitLayer(LH, RH) # Splitter layer takes as input the hidden state from both directions pred = torch.unsqueeze(pred, 0) # Add a dimension so that 'tensor_cat()' will work correctly if preds is None: preds = pred else: preds = pu.tensor_cat(preds, pred, 0) # Build a stack of tensors try: assert(len(line) - 1 == len(preds)) except: err([preds, len(line), len(preds)]) raise return preds
CARRIER = "../Carrier2.jpg" BIOMETRIC = "../FingerPrint.png" EXPECTED = "./Expected.png" biometric = cv2.imread(BIOMETRIC) carrier = cv2.imread(CARRIER) expected = cv2.cvtColor(cv2.imread(EXPECTED), cv2.COLOR_BGR2GRAY) BIOMETRIC_SIZE = biometric.shape[0] actualSize = carrier.shape corners = [[0, 0], [0, 0], [0, 0], [0, 0]] embedded = carrier.copy() em = Embedder(PASSES, KEY, CHANNELS, DEPTH, MASK) ex = Extractor(PASSES, KEY, ~MASK, SHIFT) em_psnr = [] ex_psnr = [] for iteration in range(ITERATIONS): embedded = em.embed(biometric, embedded) filename = './CarrierVsRecoved/Iteration_' + str(iteration) + '.jpg' cv2.imwrite(filename, embedded) embedded = cv2.imread(filename) em_psnr.append(cv2.PSNR(carrier, embedded)) extracted = ex.extract(embedded, corners, actualSize, BIOMETRIC_SIZE) ex_psnr.append(cv2.PSNR(expected, extracted)) print(iteration, em_psnr[-1], ex_psnr[-1])
def recognize_video(detector, embedder: Embedder, recognizer: Recognizer, detector_params='default', source=0): # инициализация видеопотока print('Starting video stream...') vs = VideoStream(src=source).start() if not is_detector(detector): raise TypeError('Incorrect type of detector') # разогрев камеры time.sleep(0.5) # запуск оценщика пропускной способности FPS fps = FPS().start() # цикл по фреймам из видео while True: frame = vs.read() if detector_params == 'default': faces_roi, boxes = detector.calc_image(frame, return_mode='both') elif type(detector) == DetectorSSD: confidence = detector_params[0] faces_roi, boxes = detector.calc_image(frame, confidence=confidence, return_mode='both') elif type(detector) == DetectorVJ or type(detector) == DetectorLBP: [scale_factor, min_neighbors] = detector_params faces_roi, boxes = detector.calc_image(frame, scale_factor=scale_factor, min_neighbors=min_neighbors, return_mode='both') elif type(detector) == DetectorHOG or type(detector) == DetectorMMOD: upsampling_times = detector_params[0] faces_roi, boxes = detector.calc_image( frame, upsampling_times=upsampling_times, return_mode='both') for i in range(len(faces_roi)): embeddings = embedder.calc_face(faces_roi[i]) name = recognizer.recognize(embeddings) start_x, start_y, end_x, end_y = boxes[i] text = '{}'.format(name) y = start_y - 10 if start_y - 10 > 10 else start_y + 10 cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), (0, 0, 255), 2) cv2.putText(frame, text, (start_x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) # обновление счетчика FPS fps.update() # показ выходного фрейма cv2.imshow('Frame', frame) key = cv2.waitKey(1) & 0xFF # завершение при нажатии 'q' if key == ord("q"): break fps.stop() print('Elasped time: {:.2f}'.format(fps.elapsed())) print('Approx. FPS: {:.2f}'.format(fps.fps())) cv2.destroyAllWindows() vs.stop()
def recognize_image(detector, embedder: Embedder, recognizer: Recognizer, image, detector_params='default', showing=True, saving_path=''): image_t = type(image) if image_t is str: image = image.replace('/', os.path.sep) image = cv2.imread(image) elif image_t is list: image = np.array(image) if not is_detector(detector): raise TypeError('Incorrect type of detector') # вызов детектора с заданными параметрами if detector_params == 'default': faces_roi, boxes = detector.calc_image(image, return_mode='both') elif type(detector) == DetectorSSD: confidence = detector_params[0] faces_roi, boxes = detector.calc_image(image, confidence=confidence, return_mode='both') elif type(detector) == DetectorVJ or type(detector) == DetectorLBP: [scale_factor, min_neighbors] = detector_params faces_roi, boxes = detector.calc_image(image, scale_factor=scale_factor, min_neighbors=min_neighbors, return_mode='both') elif type(detector) == DetectorHOG or type(detector) == DetectorMMOD: upsampling_times = detector_params[0] faces_roi, boxes = detector.calc_image( image, upsampling_times=upsampling_times, return_mode='both') # для каждого обнаруженного лица for i in range(len(faces_roi)): # получение признаков embeddings = embedder.calc_face(faces_roi[i]) # получение класса name = recognizer.recognize(embeddings) start_x, start_y, end_x, end_y = boxes[i] # рисование ограничительной рамки лица, написание имени text = '{}'.format(name) y = start_y - 10 if start_y - 10 > 10 else start_y + 10 cv2.rectangle(image, (start_x, start_y), (end_x, end_y), (255, 0, 0), 4) cv2.putText(image, text, (start_x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 0, 0), 4) if saving_path != '': cv2.imwrite(saving_path, image) # если нужно вывести изоражение на экран if showing: cv2.imshow('Image', image) cv2.waitKey(0)
carrier = gl.washBitDepth(carrier, ~GUIDE_MASK) carrier = gl.generateGuideLines(carrier, GUIDE_MASK) x1 = int(CARRIER_SIZE[0] / 2) - int(CARRIER_SIZE[0] / 4) x2 = int(CARRIER_SIZE[0] / 2) + int(CARRIER_SIZE[0] / 4) y1 = int(CARRIER_SIZE[1] / 2) - int(CARRIER_SIZE[1] / 4) y2 = int(CARRIER_SIZE[1] / 2) + int(CARRIER_SIZE[1] / 4) crop = (x1, y1, x2, y2) untouched = carrier[crop[0]:crop[2], crop[1]:crop[3]] em_psnr = [] ex_psnr = [] for passes in range(MAX_PASSES): em = Embedder(passes, KEY, CHANNELS, DEPTH, MASK) ex = Extractor(passes, KEY, ~MASK, SHIFT) embedded = em.embed(biometric, carrier) cropped = embedded[crop[0]:crop[2], crop[1]:crop[3]] margins = gl.getMargins(cropped, GUIDE_MASK) corners = gl.marginsToCorners(margins) actualSize = gl.getActualSize(cropped, margins) extracted = ex.extract(cropped, corners, actualSize, BIOMETRIC_SIZE) ex_psnr.append(cv2.PSNR(expected, extracted)) em_psnr.append(cv2.PSNR(np.uint8(cropped), np.uint8(untouched))) print(passes, em_psnr[-1], ex_psnr[-1]) cv2.imwrite('./final.png', extracted)
for pos in range(max_seq_len): for i in range(0, d_model, 2): pe[pos, i] = math.sin(pos / (10000**((2 * i) / d_model))) pe[pos, i + 1] = math.cos(pos / (10000**((2 * (i + 1)) / d_model))) self.pe = pe.unsqueeze(0) self.pe.requires_grad = False def forward(self, x) -> torch.Tensor: ret = math.sqrt(self.d_model) * x + self.pe return ret if __name__ == "__main__": train_dl, val_dl, test_dl, TEXT = get_IMDb_Dataloaders_and_TEXT( max_length=256, batch_size=24) batch = next(iter(train_dl)) net1 = Embedder(TEXT.vocab.vectors) net2 = PositionalEncoder(d_model=300, max_seq_len=256) with timer("forward"): x = batch.Text[0] x1 = net1(x) x1 = x1.to(device) x2 = net2(x1) assert x1.shape == torch.Size([24, 256, 300]) assert x2.shape == torch.Size([24, 256, 300])
def __init__( self, params, input_vocabulary, output_vocabulary, anonymizer): self.params = params self._pc = dy.ParameterCollection() if params.new_version: self.controller = Controller(output_vocabulary) else: self.controller = None # Create the input embeddings self.input_embedder = Embedder(self._pc, params.input_embedding_size, name="input-embedding", vocabulary=input_vocabulary, anonymizer=anonymizer) # Create the output embeddings self.output_embedder = Embedder(self._pc, params.output_embedding_size, name="output-embedding", vocabulary=output_vocabulary, anonymizer=anonymizer) # Create the encoder encoder_input_size = params.input_embedding_size if params.discourse_level_lstm: encoder_input_size += params.encoder_state_size / 2 self.utterance_encoder = Encoder(params.encoder_num_layers, encoder_input_size, params.encoder_state_size, self._pc) # Positional embedder for utterances attention_key_size = params.encoder_state_size if params.state_positional_embeddings: attention_key_size += params.positional_embedding_size self.positional_embedder = Embedder( self._pc, params.positional_embedding_size, name="positional-embedding", num_tokens=params.maximum_utterances) # Create the discourse-level LSTM parameters if params.discourse_level_lstm: self.discourse_lstms = du.create_multilayer_lstm_params( 1, params.encoder_state_size, params.encoder_state_size / 2, self._pc, "LSTM-t") self.initial_discourse_state = du.add_params(self._pc, tuple( [params.encoder_state_size / 2]), "V-turn-state-0") # Snippet encoder final_snippet_size = 0 if params.use_snippets and not params.previous_decoder_snippet_encoding: snippet_encoding_size = int(params.encoder_state_size / 2) final_snippet_size = params.encoder_state_size if params.snippet_age_embedding: snippet_encoding_size -= int( params.snippet_age_embedding_size / 4) self.snippet_age_embedder = Embedder( self._pc, params.snippet_age_embedding_size, name="snippet-age-embedding", num_tokens=params.max_snippet_age_embedding) final_snippet_size = params.encoder_state_size \ + params.snippet_age_embedding_size / 2 self.snippet_encoder = Encoder(params.snippet_num_layers, params.output_embedding_size, snippet_encoding_size, self._pc) token_predictor = construct_token_predictor(self._pc, params, output_vocabulary, attention_key_size, final_snippet_size, anonymizer) # 注意:此处在input增加了decoder_state_size维度 self.decoder = SequencePredictor( params, params.output_embedding_size + attention_key_size + params.decoder_state_size, self.output_embedder, self._pc, token_predictor) self.trainer = dy.AdamTrainer( self._pc, alpha=params.initial_learning_rate) self.dropout = 0.
def make_dataset(images_path='dataset'): detector = DetectorSSD() embedder = Embedder() images_path = images_path.replace('/', os.path.sep) ind_image_paths = list(paths.list_images(images_path)) # списки для лиц и имен, взаимосоответствующие по индексу known_ems = [] known_names = [] for (i, image_path) in enumerate(ind_image_paths): print(i + 1) # получение имя из пути к файлу name = image_path.split(os.path.sep)[-2] image = cv2.imread(image_path) # ОБЫЧНОЕ temp_img = image.copy() face = detector.calc_image(temp_img)[0] embeddings = embedder.calc_face(face) known_ems.append(embeddings.flatten()) known_names.append(name) # ТЕМНЕЕ 4 for i in range(1, 5): temp_img = image.copy() face = detector.calc_image(ie.darken(temp_img, i / 10)) if len(face) > 0: embeddings = embedder.calc_face(face[0]) known_ems.append(embeddings.flatten()) known_names.append(name) # СВЕТЛЕЕ 4 for i in range(1, 5): temp_img = image.copy() face = detector.calc_image(ie.brighten(temp_img, i / 10)) if len(face) > 0: embeddings = embedder.calc_face(face[0]) known_ems.append(embeddings.flatten()) known_names.append(name) # ДОЖДЬ 3 temp_img = image.copy() face = detector.calc_image(ie.add_rain(temp_img, rain_type='drizzle')) if len(face) > 0: embeddings = embedder.calc_face(face[0]) known_ems.append(embeddings.flatten()) known_names.append(name) temp_img = image.copy() face = detector.calc_image(ie.add_rain(temp_img, rain_type='heavy')) if len(face) > 0: embeddings = embedder.calc_face(face[0]) known_ems.append(embeddings.flatten()) known_names.append(name) temp_img = image.copy() face = detector.calc_image( ie.add_rain(temp_img, rain_type='torrential')) if len(face) > 0: embeddings = embedder.calc_face(face[0]) known_ems.append(embeddings.flatten()) known_names.append(name) # ТУМАН 4 for i in range(1, 5): temp_img = image.copy() face = detector.calc_image(ie.add_fog(temp_img, i / 10)) if len(face) > 0: embeddings = embedder.calc_face(face[0]) known_ems.append(embeddings.flatten()) known_names.append(name) embeddings = {'embeddings': known_ems, 'names': known_names} f = open('weather_ems/ems_weather_4.pickle', 'wb') f.write(pickle.dumps(embeddings)) f.close() print(len(known_ems), len(known_names)) return known_ems, known_names