def run(self): self.isRunning = True self.camera.stream_on() #print(f'camera: {self.camera}') while self.isRunning: if self.exposure_changed == True: self.camera.ExposureTime.set(self.exposure) self.exposure_changed = False img = self.camera.data_stream[0].get_image() if img == None: continue self.lastFrame = img.get_numpy_array() self.frame_signal.emit(self.lastFrame) if self.isProfile or self.isCalcParams: proc = self.lastFrame[self.img_ranges[2]:self.img_ranges[3],\ self.img_ranges[0]:self.img_ranges[1]] x, y = np.sum(proc, axis=0), np.sum(proc, axis=1) if self.isProfile: self.distrs_signal.emit(x, y) if self.isCalcParams: optx = fit(x) opty = fit(y) self.params_signal.emit(x, y, optx, opty) if self.drv is not None: self.drv.update(self.pvs['pos_x'], 10*self.calibr[0]*optx[1]) self.drv.update(self.pvs['pos_y'], 10*self.calibr[1]*opty[1]) self.camera.stream_off()
def get_detector_image_generator(labels, width, height, augmenter=None, area_threshold=0.5): """Generated augmented (image, lines) tuples from a list of (filepath, lines, confidence) tuples. Confidence is not used right now but is included for a future release that uses semi-supervised data. Args: labels: A list of (image, lines, confience) tuples. augmenter: An augmenter to apply to the images. width: The width to use for output images height: The height to use for output images area_threshold: The area threshold to use to keep characters in augmented images. """ labels = labels.copy() for index in itertools.cycle(range(len(labels))): if index == 0: random.shuffle(labels) image_filepath, lines = labels[index] image = tools.read(image_filepath) if augmenter is not None: image, lines = tools.augment(boxes=lines, boxes_format='lines', image=image, area_threshold=area_threshold, augmenter=augmenter) image, scale = tools.fit(image, width=width, height=height, mode='letterbox', return_scale=True) lines = tools.adjust_boxes(boxes=lines, boxes_format='lines', scale=scale) bboxes = [line[0] for line in lines] words = [line[1] for line in lines] words = ''.join(words) yield image[np.newaxis, ...], np.array(bboxes)[np.newaxis, ...],\ np.array(words)[np.newaxis, ... ], np.ones((image.shape[0], image.shape[1]), np.float32)[np.newaxis, ...],\ np.ones(len(words), np.float32)[np.newaxis, ...]
def estimate_matrix(logits_matrix, model_save_dir): transition_matrix_group = np.empty( (args.basis, args.num_classes, args.num_classes)) idx_matrix_group = np.empty((args.num_classes, args.basis)) a = np.linspace(97, 99, args.basis) a = list(a) for i in range(len(a)): percentage = a[i] index = int(i) logits_matrix_ = copy.deepcopy(logits_matrix) transition_matrix, idx = tools.fit(logits_matrix_, args.num_classes, percentage, True) transition_matrix = norm(transition_matrix) idx_matrix_group[:, index] = np.array(idx) transition_matrix_group[index] = transition_matrix idx_group_save_dir = model_save_dir + '/' + 'idx_group.npy' group_save_dir = model_save_dir + '/' + 'T_group.npy' np.save(idx_group_save_dir, idx_matrix_group) np.save(group_save_dir, transition_matrix_group) return idx_matrix_group, transition_matrix_group
out = F.softmax(out, dim=1) out = out.cpu() if index <= index_num: A[epoch][index * args.batch_size:(index + 1) * args.batch_size, :] = out else: A[epoch][index_num * args.batch_size, len(train_data), :] = out val_acc_array = np.array(val_acc_list) model_index = np.argmax(val_acc_array) A_save_dir = prob_save_dir + '/' + 'prob.npy' np.save(A_save_dir, A) prob_ = np.load(A_save_dir) transition_matrix_ = tools.fit(prob_[model_index, :, :], args.num_classes, estimate_state) transition_matrix = tools.norm(transition_matrix_) matrix_path = matrix_save_dir + '/' + 'transition_matrix.npy' np.save(matrix_path, transition_matrix) T = torch.from_numpy(transition_matrix).float().cuda() # initial parameters estimate_model_path = model_save_dir + '/' + 'epoch_%s.pth' % (model_index + 1) estimate_model_path = torch.load(estimate_model_path) model.load_state_dict(estimate_model_path) print('Estimate finish.....Training......') for epoch in range(args.n_epoch):
def get_detector_image_generator(labels, width, height, augmenter=None, area_threshold=0.5, focused=False, min_area=None): """Generated augmented (image, lines) tuples from a list of (filepath, lines, confidence) tuples. Confidence is not used right now but is included for a future release that uses semi-supervised data. Args: labels: A list of (image, lines, confience) tuples. augmenter: An augmenter to apply to the images. width: The width to use for output images height: The height to use for output images area_threshold: The area threshold to use to keep characters in augmented images. min_area: The minimum area for a character to be included. focused: Whether to pre-crop images to width/height containing a region containing text. """ labels = labels.copy() for index in itertools.cycle(range(len(labels))): if index == 0: random.shuffle(labels) image_filepath, lines, confidence = labels[index] # print(image_filepath) image = tools.read(image_filepath) if augmenter is not None: image, lines = tools.augment(boxes=lines, boxes_format='lines', image=image, area_threshold=area_threshold, min_area=min_area, augmenter=augmenter) if focused: boxes = [tools.combine_line(line)[0] for line in lines] if boxes: selected = np.array(boxes[np.random.choice(len(boxes))]) left, top = selected.min(axis=0).clip(0, np.inf).astype('int') if left > 0: left -= np.random.randint(0, min(left, width / 2)) if top > 0: top -= np.random.randint(0, min(top, height / 2)) image, lines = tools.augment( boxes=lines, augmenter=imgaug.augmenters.Sequential([ imgaug.augmenters.Crop(px=(int(top), 0, 0, int(left))), imgaug.augmenters.CropToFixedSize(width=width, height=height, position='right-bottom') ]), boxes_format='lines', image=image, min_area=min_area, area_threshold=area_threshold) image, scale = tools.fit(image, width=width, height=height, mode='letterbox', return_scale=True) lines = tools.adjust_boxes(boxes=lines, boxes_format='lines', scale=scale) yield image, lines, confidence
def real_data_generator(labels, width, height, augmenter=None, area_threshold=0.5, ): labels = labels.copy() for index in itertools.cycle(range(len(labels))): image_filepath, lines = labels[index] image = tools.read(image_filepath) if augmenter is not None: image, lines = tools.augment(boxes=lines, boxes_format='lines', image=image, area_threshold=area_threshold, augmenter=augmenter) image, scale = tools.fit(image, width=width, height=height, mode='letterbox', return_scale=True) lines = tools.adjust_boxes(boxes=lines, boxes_format='lines', scale=scale) confidence_mask = np.zeros((image.shape[0], image.shape[1]), np.float32) confidences = [] # character_bboxes = np.array([]).reshape(0, 4, 2) # new_words = [] lines_label = [] detector = Detector() if len(lines)==1: lines = lines[0] for i, line in enumerate(lines): word_label = [] word_bbox, word = line[0], line[1] word = word.replace(',', '') word_bbox = np.float32(word_bbox) if len(word_bbox) > 0: for _ in range(len(word_bbox)): if word == '###' or len(word.strip()) == 0: cv2.fillPoly(confidence_mask, [np.int32(word_bbox)], (0)) pursedo_bboxes, bbox_region_scores, confidence = inference_character_box(detector, image, word, word_bbox) confidences.append(confidence) cv2.fillPoly(confidence_mask, [np.int32(word_bbox)], (confidence)) for j in range(len(pursedo_bboxes)): if j>len(word)-1: continue word_label.append((pursedo_bboxes[j], word[j])) lines_label.append(word_label) # new_words.append(word) # character_bboxes = np.concatenate((character_bboxes, pursedo_bboxes), 0) # character_bboxes.append(pursedo_bboxes) yield image, lines_label, 1
def __init__(self,obj,domain=None,N=None,rtol=None): """ Initilize the cheb obj can be one of * a python callable * a numpy ufunc * a string (using the numpy namespace) * a ndarray to use as the cheb coeffs * an existing tools.ChebyshevPolynomial poly object domain is a tuple (low,high) of the bounds of the function if N is specified, use that number of points rtol is the relative tolerance in the coefficients, should be approximately the accuracy of the resulting cheb """ self.mapper = lambda x: x self.imapper = lambda x: x self.domain = (-1,1) if domain is not None: #if we were passed a domain a,b = domain self.domain = (a,b) #mapper maps from (a,b) to (-1,1) self.mapper = tools.gen_mapper(a,b) #imapper maps from (-1,1) to (a,b) self.imapper = tools.gen_imapper(a,b) #by default use numpy float tolerance self.rtol = rtol or DEFAULT_TOL self._constructed = False #Here I have a somewhat inelegant casing out # to allow initilization overloading if isinstance(obj, self.__class__): #if we have a chebfun, just copy it self._from_cheb(other_cheb=obj) elif isinstance(obj,tools.ChebyshevPolynomial): #we have a chebyshev poly self._from_poly(poly=obj) elif isinstance(obj,np.ndarray): #use the ndarray as our coefficients self._from_array(array=obj) elif isinstance(obj,str): #we have a string, eval it in the numpy namespace self._from_string(string=obj) elif isinstance(obj,(np.ufunc,np.vectorize)): # we're good to go self._from_ufunc(ufunc=obj) elif callable(obj): #try to vectorize a general callable self._from_func(func=obj) else: raise TypeError, "I don't understand your func: {}".format(obj) if N is not None: #if the user passed in an N, assume that's what he wants #we need the function on the interval (-1,1) func = lambda x: self.func(self.imapper(x)) coeffs = tools.fit(func, N) self.poly = tools.ChebyshevPolynomial(coeffs,self.domain) self._constructed = True
def main(): print('Estimate transition matirx......Waiting......') for epoch in range(args.n_epoch_estimate): print('epoch {}'.format(epoch + 1)) model.train() train_loss = 0. train_acc = 0. val_loss = 0. val_acc = 0. for batch_x, batch_y in train_loader: batch_x = batch_x.cuda() batch_y = batch_y.cuda() optimizer_es.zero_grad() out = model(batch_x, revision=False) loss = loss_func_ce(out, batch_y) train_loss += loss.item() pred = torch.max(out, 1)[1] train_correct = (pred == batch_y).sum() train_acc += train_correct.item() loss.backward() optimizer_es.step() torch.save(model.state_dict(), model_save_dir + '/' + 'epoch_%d.pth' % (epoch + 1)) print('Train Loss: {:.6f}, Acc: {:.6f}'.format( train_loss / (len(train_data)) * args.batch_size, train_acc / (len(train_data)))) with torch.no_grad(): model.eval() for batch_x, batch_y in val_loader: batch_x = batch_x.cuda() batch_y = batch_y.cuda() out = model(batch_x, revision=False) loss = loss_func_ce(out, batch_y) val_loss += loss.item() pred = torch.max(out, 1)[1] val_correct = (pred == batch_y).sum() val_acc += val_correct.item() print('Val Loss: {:.6f}, Acc: {:.6f}'.format( val_loss / (len(val_data)) * args.batch_size, val_acc / (len(val_data)))) val_acc_list.append(val_acc / (len(val_data))) with torch.no_grad(): model.eval() for index, (batch_x, batch_y) in enumerate(estimate_loader): batch_x = batch_x.cuda() out = model(batch_x, revision=False) out = F.softmax(out, dim=1) out = out.cpu() if index <= index_num: A[epoch][index * args.batch_size:(index + 1) * args.batch_size, :] = out else: A[epoch][index_num * args.batch_size, len(train_data), :] = out val_acc_array = np.array(val_acc_list) model_index = np.argmax(val_acc_array) A_save_dir = prob_save_dir + '/' + 'prob.npy' np.save(A_save_dir, A) prob_ = np.load(A_save_dir) transition_matrix_ = tools.fit(prob_[model_index, :, :], args.num_classes, estimate_state) transition_matrix = tools.norm(transition_matrix_) matrix_path = matrix_save_dir + '/' + 'transition_matrix.npy' np.save(matrix_path, transition_matrix) T = torch.from_numpy(transition_matrix).float().cuda() True_T = tools.transition_matrix_generate(noise_rate=args.noise_rate, num_classes=args.num_classes) estimate_error = tools.error(T.cpu().numpy(), True_T) print('The estimation error is %s' % (estimate_error)) # initial parameters estimate_model_path = model_save_dir + '/' + 'epoch_%s.pth' % ( model_index + 1) estimate_model_path = torch.load(estimate_model_path) model.load_state_dict(estimate_model_path) print('Estimate finish.....Training......') val_acc_list_r = [] for epoch in range(args.n_epoch): print('epoch {}'.format(epoch + 1)) # training----------------------------- train_loss = 0. train_acc = 0. val_loss = 0. val_acc = 0. eval_loss = 0. eval_acc = 0. scheduler.step() model.train() for batch_x, batch_y in train_loader: batch_x = batch_x.cuda() batch_y = batch_y.cuda() optimizer.zero_grad() out = model(batch_x, revision=False) prob = F.softmax(out, dim=1) prob = prob.t() loss = loss_func_reweight(out, T, batch_y) out_forward = torch.matmul(T.t(), prob) out_forward = out_forward.t() train_loss += loss.item() pred = torch.max(out_forward, 1)[1] train_correct = (pred == batch_y).sum() train_acc += train_correct.item() loss.backward() optimizer.step() with torch.no_grad(): model.eval() for batch_x, batch_y in val_loader: model.eval() batch_x = batch_x.cuda() batch_y = batch_y.cuda() out = model(batch_x, revision=False) prob = F.softmax(out, dim=1) prob = prob.t() loss = loss_func_reweight(out, T, batch_y) out_forward = torch.matmul(T.t(), prob) out_forward = out_forward.t() val_loss += loss.item() pred = torch.max(out_forward, 1)[1] val_correct = (pred == batch_y).sum() val_acc += val_correct.item() torch.save(model.state_dict(), model_save_dir + '/' + 'epoch_r%d.pth' % (epoch + 1)) print('Train Loss: {:.6f}, Acc: {:.6f}'.format( train_loss / (len(train_data)) * args.batch_size, train_acc / (len(train_data)))) print('Val Loss: {:.6f}, Acc: {:.6f}'.format( val_loss / (len(val_data)) * args.batch_size, val_acc / (len(val_data)))) val_acc_list_r.append(val_acc / (len(val_data))) with torch.no_grad(): model.eval() for batch_x, batch_y in test_loader: batch_x = batch_x.cuda() batch_y = batch_y.cuda() out = model(batch_x, revision=False) loss = loss_func_ce(out, batch_y) eval_loss += loss.item() pred = torch.max(out, 1)[1] eval_correct = (pred == batch_y).sum() eval_acc += eval_correct.item() print('Test Loss: {:.6f}, Acc: {:.6f}'.format( eval_loss / (len(test_data)) * args.batch_size, eval_acc / (len(test_data)))) val_acc_array_r = np.array(val_acc_list_r) reweight_model_index = np.argmax(val_acc_array_r) reweight_model_path = model_save_dir + '/' + 'epoch_r%s.pth' % ( reweight_model_index + 1) reweight_model_path = torch.load(reweight_model_path) model.load_state_dict(reweight_model_path) nn.init.constant_(model.T_revision.weight, 0.0) print('Revision......') for epoch in range(args.n_epoch_revision): print('epoch {}'.format(epoch + 1)) # training----------------------------- train_loss = 0. train_acc = 0. val_loss = 0. val_acc = 0. eval_loss = 0. eval_acc = 0. model.train() for batch_x, batch_y in train_loader: batch_x = batch_x.cuda() batch_y = batch_y.cuda() optimizer_revision.zero_grad() out, correction = model(batch_x, revision=True) prob = F.softmax(out, dim=1) prob = prob.t() loss = loss_func_revision(out, T, correction, batch_y) out_forward = torch.matmul((T + correction).t(), prob) out_forward = out_forward.t() train_loss += loss.item() pred = torch.max(out_forward, 1)[1] train_correct = (pred == batch_y).sum() train_acc += train_correct.item() loss.backward() optimizer_revision.step() with torch.no_grad(): model.eval() for batch_x, batch_y in val_loader: batch_x = batch_x.cuda() batch_y = batch_y.cuda() out, correction = model(batch_x, revision=True) prob = F.softmax(out, dim=1) prob = prob.t() loss = loss_func_revision(out, T, correction, batch_y) out_forward = torch.matmul((T + correction).t(), prob) out_forward = out_forward.t() val_loss += loss.item() pred = torch.max(out_forward, 1)[1] val_correct = (pred == batch_y).sum() val_acc += val_correct.item() estimate_error = tools.error(True_T, (T + correction).cpu().detach().numpy()) print('Estimate error: {:.6f}'.format(estimate_error)) print('Train Loss: {:.6f}, Acc: {:.6f}'.format( train_loss / (len(train_data)) * args.batch_size, train_acc / (len(train_data)))) print('Val Loss: {:.6f}, Acc: {:.6f}'.format( val_loss / (len(val_data)) * args.batch_size, val_acc / (len(val_data)))) with torch.no_grad(): for batch_x, batch_y in test_loader: batch_x = batch_x.cuda() batch_y = batch_y.cuda() out, _ = model(batch_x, revision=True) loss = loss_func_ce(out, batch_y) eval_loss += loss.item() pred = torch.max(out, 1)[1] eval_correct = (pred == batch_y).sum() eval_acc += eval_correct.item() print('Test Loss: {:.6f}, Acc: {:.6f}'.format( eval_loss / (len(test_data)) * args.batch_size, eval_acc / (len(test_data))))