def save_stats(self): stats = { 'args': self.args, 'train_stats': self.all_train_stats, 'valid_stats': self.all_valid_stats, 'test_stats': self.all_test_stats, 'best_valid_stats': self.best_valid_stats, 'best_epoch': self.best_epoch } save( stats, os.path.join(self.saving_path, 'stats', self.get_saving_file_name())) csv_path = os.path.join(self.saving_path, 'csv', self.get_saving_file_name()).replace( '.pt', '.csv') dirname = os.path.dirname(csv_path) if not os.path.exists(dirname): os.makedirs(dirname) with open(csv_path, 'w') as f: for stat in self.all_test_stats[self.best_epoch - 1]: for n in stat: f.write(f'{n:.4f},') f.write('\n') f.write(str(self.args)) f.write('\n')
def recuperaRatioDilatacao(contornos, imgOriginal, identificador): ratio = 0.1 for i, c in enumerate(contornos): x, y, w, h = cv2.boundingRect(c) b = 10 roi = imgOriginal[y-b:y + h+b, x-b:x + w+b] resized = roi.copy() #resized = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) retval, resized = cv2.threshold(resized, 120, 255, type = cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) utils.save('D{}.jpg'.format(i), roi, id=identificador) preResized = resized.copy() print('IMAGEM: ' + str(i)) print('=======================') for x in range(0, 7): print('Processando Ratio: ' + str(ratio)) resized = utils.dilatation(preResized, ratio=ratio) utils.save('ratio{}_{}.jpg'.format(i,x), resized, id=identificador) im2, contours2, hierarchy = cv2.findContours(resized, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours2 = removeContornosPqnos(contours2) if (len(contours2) == 1): break else: ratio += 0.3 print() print('Ratio encontrado: ' + str(ratio)) return ratio
def printaContornoEncontrado(img, cnts, identificador): imgContorno = img.copy() for idx1,c in enumerate(cnts): cv2.drawContours(imgContorno, [c], -1, utils.color(), 4) utils.save('contorno.jpg', imgContorno, id=identificador)
def printaOrdem(img, contornos, identificador): imgSource = img.copy() for i, c in enumerate(contornos): x, y, w, h = cv2.boundingRect(c) putText(imgSource, str(i), (x,y)) utils.save('ordem.jpg', imgSource, id=identificador)
def aumentaCanvas(img, identificador): shape = tuple([500+x for x in img.shape]) novaMat = np.zeros(shape, dtype = "uint8") im2, contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for i2, c2 in enumerate(contours): #c2 = c2 + [300,300] cv2.drawContours(novaMat, [c2], -1, 255, -1) utils.save('redimensionada.jpg', novaMat, id=identificador) return novaMat
def save_stats(self): stats = { 'args': self.args, 'train_stats': self.all_train_stats, 'valid_stats': self.all_valid_stats, 'test_stats': self.all_test_stats, 'best_valid_stats': self.best_valid_stats, 'best_epoch': self.best_epoch } save( stats, os.path.join(self.saving_path, 'stats', self.get_saving_file_name()))
def getMoment(cnts, idx, identificador): x, y, w, h = cv2.boundingRect(cnts) outline = np.ones((h, w), dtype="uint8") outline = cv2.copyMakeBorder(outline, 15, 15, 15, 15, cv2.BORDER_CONSTANT, value=0) cv2.drawContours(outline, [cnts], -1, 255, -1) outline = cv2.bitwise_not(outline) utils.save('mahota_{}.jpg'.format(idx), outline, id=identificador) moments = mahotas.features.zernike_moments(outline, 62) return moments
def transformaItem(square2, altura1, largura1, identificador, idx2): square2 = utils.resize(square2, width=largura1, height=altura1) im2, contours2, hierarchy = cv2.findContours(square2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(contours2, key=functionSort, reverse=True)[0] novaMat = np.zeros(square2.shape, dtype="uint8") cv2.drawContours(novaMat, [cnts], -1, 255, -1) utils.save('_img_{}.jpg'.format(idx2), novaMat, id=identificador) #recarrega a imagem do disco, suaviza e contorna path = utils.buildPath(identificador, path="_img_" + str(idx2) + ".jpg") imgGray = cv2.imread(path, cv2.COLOR_BGR2GRAY) #utils.save('debug_{}_antes.jpg'.format(idx2), imgGray, id=identificador) imgGray = cv2.medianBlur(imgGray, 5) utils.save('__{}_depois.jpg'.format(idx2), imgGray, id=identificador) retval, imgGray = cv2.threshold(imgGray, 2, 255, type=cv2.THRESH_BINARY | cv2.THRESH_OTSU) im2, contours, hierarchy = cv2.findContours(imgGray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts2 = contours[0] debugMat = np.zeros(imgGray.shape, dtype="uint8") cv2.drawContours(debugMat, [cnts2], -1, 255, -1) utils.save('debug_{}.jpg'.format(idx2), debugMat, id=identificador) return cnts2
def validaAssinaturaCnh(cnhColor, square1, identificador): cnhColor = utils.removeSombras(cnhColor) utils.save('cnh_semSombra.jpg', cnhColor, id=identificador) imgGray = cnhColor #cv2.cvtColor(cnhColor, cv2.IMREAD_GRAYSCALE) #imgGray = cv2.medianBlur(imgGray, 21) imgTh, contours, hierarchy = extraiContornos(imgGray, identificador) contours, resized = utils.ajustaEspacosContorno(contours, imgTh) utils.save('cnh_resized.jpg', resized, id=identificador) cnts = contours[0] novaMat = np.zeros(imgGray.shape, dtype = "uint8") cv2.drawContours(novaMat, [cnts], -1, 255, -1) xA, yA, wA, hA = cv2.boundingRect(cnts) square = novaMat[yA :yA + hA, xA : xA + wA ] utils.save('cnh_square.jpg', square, id=identificador) h, w = square1.shape resized = utils.resize(square, width=w, height=h) utils.save('_img_6.jpg', resized, id=identificador) path = utils.buildPath(identificador, path="_img_6.jpg") print("Novo path " + path) imgGray = cv2.imread(path, cv2.COLOR_BGR2GRAY) print(imgGray) #imgGray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY) retval, imgGray = cv2.threshold(imgGray, 2, 255, type = cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) imgGray, densidade = utils.removeContornosPqnosImg(imgGray) im2, contours, hierarchy = cv2.findContours(imgGray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #contours, resized = utils.ajustaEspacosContorno(contours, imgTh) cnts2 = contours[0] print("Total de contornos CNH ANTES: " + str(len(cnts))) print("Total de contornos CNH DEPOIS: " + str(len(cnts2))) return cnts2, resized
def escreveImagem(self, imagemBase64, cnhBase64, cnhDimencoes, identificador): with open(utils.buildPath(identificador, path=names.ORIGINAL), "wb") as fh: fh.write(base64.b64decode(imagemBase64)) if (cnhBase64 != ""): pathCnhFull = utils.buildPath(identificador, path=names.CNH_ORIGINAL) with open(pathCnhFull, "wb") as fh: fh.write(base64.b64decode(cnhBase64)) #extrai assinatura CNH cnhFull = cv2.imread(pathCnhFull, cv2.IMREAD_GRAYSCALE) x = int(cnhDimencoes[0]) y = int(cnhDimencoes[1]) w = int(cnhDimencoes[2]) h = int(cnhDimencoes[3]) cnhAss = cnhFull[y:y + h, x:x + w] utils.save(names.CNH_ASSINATURA, cnhAss, id=identificador)
def recuperaAreaAssinada(canny_img, imgOriginal, identificador): color = canny_img.copy() canny_img = cv2.cvtColor(canny_img, cv2.COLOR_BGR2GRAY) canny_img, contours, hierarchy = extraiContornos(canny_img, identificador) contours = sorted(contours, key=functionSort, reverse=True)[0:5] try: hierarchy = hierarchy[0] except: hierarchy = [] height, width = canny_img.shape min_x, min_y = width, height max_x = max_y = 0 print('h{} w{}'.format(height, width)) # computes the bounding box for the contour, and draws it on the frame, for contour, hier in zip(contours, hierarchy): if cv2.contourArea(contour) > 400: (x,y,w,h) = cv2.boundingRect(contour) min_x, max_x = min(x, min_x), max(x+w, max_x) min_y, max_y = min(y, min_y), max(y+h, max_y) if max_x - min_x > 0 and max_y - min_y > 0: cv2.rectangle(canny_img, (min_x, min_y), (max_x, max_y), (255, 0, 0), 2) m = 30 #print('{} x={} - y{}'.format(i,x,y)) a = min_y-m if min_y-m > 0 else 0 b = max_y+m if max_y+m <= height else height c = min_x-m if min_x-m > 0 else 0 d = max_x+m if max_x+m < width else width imgGray = color[a:b, c:d] imgOriginal = imgOriginal[a:b, c:d] utils.save('contornado.jpg', imgGray, id=identificador) return imgOriginal, imgGray
def plot_mean_var(y, hs, t_axis, fol, k_type='triangular'): if not op.isfile(op.join(fol, 'ests_mean_{}.pkl'.format(k_type))): ests_mean, ests_var = {}, {} for h in hs: ests_mean[h] = est_mean(y, h, t_axis, k_type) ests_var[h] = est_var(y, ests_mean[h], h, t_axis, k_type) utils.save(ests_mean, op.join(fol, 'ests_mean_{}.pkl'.format(k_type))) utils.save(ests_var, op.join(fol, 'ests_var_{}.pkl'.format(k_type))) else: ests_mean = utils.load(op.join(fol, 'ests_mean_{}.pkl'.format(k_type))) ests_var = utils.load(op.join(fol, 'ests_var_{}.pkl'.format(k_type))) boynton_colors = ["red", "green", "yellow", "magenta", "pink", "orange", "brown", "gray"] t = range(len(y)) plt.figure() plt.plot(t, y, 'b', label='real') for h, color in zip(hs, boynton_colors): # plt.errorbar(t, ests_mean[h], c=color, yerr=np.power(ests_var[h], 0.5), label='w {}'.format(h)) plt.plot(t, ests_mean[h], color, label='w {}'.format(h)) error = np.power(ests_var[h], 0.5) plt.fill_between(t, ests_mean[h] - error, ests_mean[h] + error, alpha=0.2, edgecolor=color, facecolor=color) plt.legend() plt.savefig(op.join(fol, 'first_vertice_h_var_k_{}.jpg'.format(k_type)))
def extraiContornos(imgGray, identificador): utils.save('cnh_antesTh.jpg', imgGray, id=identificador) retval, imgGray = cv2.threshold(imgGray, 2, 255, type = cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) utils.save('cnh_postTh.jpg', imgGray, id=identificador) imgGray, densidade = utils.removeContornosPqnosImg(imgGray) utils.save('cnh_novosContornos.jpg', imgGray, id=identificador) imgGray = utils.dilatation(imgGray, ratio=0.2) im2, contours, hierarchy = cv2.findContours(imgGray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) return imgGray, contours, hierarchy
args.epochs = 1 args.iid = 0 args.unequal = 0 args.num_users = 100 args.rounds = 500 # ================================= fixed_seed(False) # print experiment details exp_details(args) # load dataset and initialize user groups train_ds, test_ds, user_groups = get_dataset(args) # build users models models = initialize_models(args, same=True) topology = central_graph(models) # include physical edge devices (count < 1 to only use simulated nodes) edge = edge_devices(args, count=-1) # build the network graph graph = network_graph(topology, models, train_ds, test_ds, user_groups, args, edge=edge) train_logs = graph.collaborative_training(learner=fedavg, args=args) save(f"fl_logs_{args.num_users}", train_logs) info = {'xlabel': "Rounds", 'title': "Accuracy. vs. No. of rounds"} plot_train_history(train_logs, metric='accuracy', measure="mean") print("END.")
from src.p2p import Graph from src.plots import plot_train_history from src.utils import load_conf, fixed_seed, save if __name__ == '__main__': # load experiment configuration from CLI arguments args = load_conf() fixed_seed(True) train_logs = Graph.centralized_training(args, inference=True) save(f"train_logs_CL", train_logs) info = {'xlabel': "Rounds", 'title': "Accuracy. vs. No. of Epochs"} plot_train_history(train_logs, metric='accuracy', measure="mean") print("END.")
def train(self): extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): g_optim = tf.train.AdamOptimizer(self.flags.lr, beta1 = self.flags.beta1) \ .minimize(self.g_loss, var_list = self.g_vars) e_optim = tf.train.AdamOptimizer(self.flags.lr, beta1 = self.flags.beta1) \ .minimize(self.e_loss, var_list = self.e_vars) # g_optim = tf.train.AdagradOptimizer(self.flags.lr).minimize(self.g_loss, var_list = self.g_vars) # e_optim = tf.train.AdagradOptimizer(self.flags.lr).minimize(self.e_loss, var_list = self.e_vars) tf.global_variables_initializer().run() # merge summary sum_total = tf.summary.merge([self.im_sum, self.im_hat_sum, self.recon_im_sum, self.g_loss_sum, self.KL_fake_g_loss_sum, self.e_loss_sum,self.KL_fake_e_loss_sum, self.KL_real_e_loss_sum]) if hasattr(self, 'match_x_e_loss_sum'): sum_total = tf.summary.merge([sum_total, self.match_x_e_loss_sum]) if hasattr(self, 'match_z_e_loss_sum'): sum_total = tf.summary.merge([sum_total, self.match_z_e_loss_sum]) if hasattr(self, 'match_z_g_loss_sum'): sum_total = tf.summary.merge([sum_total, self.match_z_g_loss_sum]) if hasattr(self, 'match_x_g_loss_sum'): sum_total = tf.summary.merge([sum_total, self.match_x_g_loss_sum]) writer = tf.summary.FileWriter("%s/ge_GAN_log_%s"%(self.flags.checkpoint_dir, self.flags.dataset_name), self.sess.graph) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord = coord ) could_load, checkpoint_counter = load(self.saver, self.sess, self.flags) if could_load: counter = checkpoint_counter print(" [*] Load SUCCESS") else: counter = 0 print(" [!] Load failed...") for i in xrange(counter, self.flags.iter): i += 1 for iter_e in range(self.updates['e']['num_updates']): if iter_e < (self.updates['e']['num_updates'] -1 ): self.sess.run([e_optim]) else: # run with loss # self.sess.run([e_optim]) _, e_loss_, KL_real_e_loss_, KL_fake_e_loss_ = self.sess.run( [e_optim, self.e_loss, self.KL_real_e_loss, self.KL_fake_e_loss]) for iter_g in range(self.updates['g']['num_updates']): if iter_g < (self.updates['g']['num_updates'] -1): self.sess.run([g_optim]) else : # run with summary and loss # self.sess.run([g_optim]) _, sum_total_, g_loss_, KL_fake_g_loss_ = self.sess.run( [g_optim, sum_total, self.g_loss, self.KL_fake_g_loss]) if np.mod(i,20) == 0: writer.add_summary(sum_total_, i) print("iteration: [%2d], g_loss: %.8f, e_loss: %.8f" % (i, g_loss_, e_loss_)) print ('fake/real_ e: ', KL_fake_e_loss_, '\\', KL_real_e_loss_) print ('fake_g: ', KL_fake_g_loss_) print('**************************') if np.mod(i,self.flags.save_iter) == 0 or i == self.flags.iter: # try to sample and save model [gt_im, recon_im] = self.sess.run([self.im, self.recon_im_test]) save_images(self.flags, gt_im, i, 'GT') save_images(self.flags, recon_im, i, 'recon') save(self.saver, self.sess, self.flags, i) print ('saved once ...')
def extrai(path, identificador): color = cv2.imread(path, -1) color = cv2.resize(color, (0, 0), fx = 0.3, fy = 0.3) imgOriginal = color.copy() color = utils.removeSombras(color) utils.save('semSombra.jpg', color, id=identificador) #imgOriginal, color = recuperaAreaAssinada(color.copy(), imgOriginal, identificador) #utils.save('antesGray.jpg', color, id=identificador) imgGray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY) imgPbOriginal = imgGray.copy() utils.save('pb1.jpg', imgGray, id=identificador) #imgGray = rotate_bound(imgGray, 90) #utils.save('pb2.jpg', imgGray) #imgGray = cv2.blur(imgGray, (blurI, blurI)) #utils.save('blur.jpg', imgGray) utils.save('AntesThr.jpg', imgGray, id=identificador) imgGray, contours, hierarchy = extraiContornos(imgGray, identificador) utils.save('thr.jpg', imgGray, id=identificador) cnts2 = sorted(contours, key=functionSort, reverse=True)[0:5] printaContornoEncontrado(imgOriginal, cnts2, identificador) cnts2 = sorted(cnts2, key=functionSortPrimeiroPapel) printaOrdem(imgOriginal, cnts2, identificador) originalEmGray = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2GRAY) #originalHisto = cv2.equalizeHist(originalEmGray) originalHisto = originalEmGray lista = dict() cntArr = dict() #ratioDilatacao = recuperaRatioDilatacao(cnts2, imgPbOriginal, identificador) for i, c in enumerate(cnts2): x, y, w, h = cv2.boundingRect(c) b = 0 #print('{} x={} - y{}'.format(i,x,y)) utils.save('imgPbSemSombra2-{}.jpg'.format(i), imgPbOriginal, id=identificador) roi = imgPbOriginal[y-b:y + h+b, x-b:x + w+b] utils.save('roi_{}.jpg'.format(i), roi, id=identificador) #utils.save('_1_hist_{}.jpg'.format(i), roi) #roi = utils.resize(roi, width=300, height=300) resized = roi.copy() #resized = cv2.blur(resized, (blurI,blurI)) #utils.save('__{}_blur1.jpg'.format(i), resized) #resized = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) #resized = cv2.blur(resized, (5,5)) retval, resized = cv2.threshold(resized, 120, 255, type = cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) resized = utils.removeContornosPqnosImg(resized) utils.save('t_{}.jpg'.format(i), resized, id=identificador) cv2.waitKey(0) #print('ratioDilatacao ' + str(ratioDilatacao)) resized = utils.dilatation(resized, ratio=0.3) utils.save('t1_{}.jpg'.format(i), resized, id=identificador) im2, contours2, hierarchy = cv2.findContours(resized, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) print('Ajustando espacos') contours2, resized = ajustaEspacosContorno(contours2, resized) print('espacos ajustados') cnts = sorted(contours2, key=functionSort, reverse=True)[0] #roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) novaMat = np.zeros(roi.shape, dtype = "uint8") cv2.drawContours(novaMat, [cnts], -1, 255, -1) #novaMat = cv2.resize(novaMat, (200,200), interpolation = cv2.INTER_AREA) #lista[i] = mahotas.features.zernike_moments(novaMat, 21) lista[i] = cnts cntArr[i] = cnts utils.save('_img_{}.jpg'.format(i), novaMat, id=identificador) #utils.show(color) hd = cv2.createHausdorffDistanceExtractor() sd = cv2.createShapeContextDistanceExtractor() out = "" sizeOut = "" resultadoApi = True imgResultado = imgOriginal.copy() for idx1 in range(0,1): #recupera apenas a primeira imagem e a compara com as outras item1 = lista[idx1] altura1, largura1 = calculaAlturaLargura(item1) soma = 0 for idx2 in range(0,5): item2 = lista[idx2] altura2, largura2 = calculaAlturaLargura(item2) #sizeOut += 'Altura {} - {} = {} / {}\n'.format(altura1, altura2, abs(altura1 - altura2), calcPercentual(largura1, largura2)) #sizeOut += 'Largura {} - {} = {} / {}\n'.format(largura1, largura2, abs(largura1 - largura2), calcPercentual(largura1, largura2)) sizeOut += 'Dimensao {} x {} \n'.format(largura2, altura2) tamanhoCompativel = alturaLarguraCompativel(altura1, largura1, altura2, largura2) #match = hd.computeDistance(item1, item2) #match = cv2.matchShapes(cntArr[idx1], cntArr[idx2], 1, 0.0) ida = sd.computeDistance(item1, item2) volta = sd.computeDistance(item2, item1) #ida = dist.euclidean(item1, item2) #volta = dist.euclidean(item2, item1) ida = round(ida, 5) volta = round(volta, 5) out += '{} vs {} ({}) == {} - {}\n'.format(idx1, idx2, tamanhoCompativel, ida, volta) #BGR if ( idx2 == 0 ): imgResultado = contorna(imgResultado, cnts2[idx2], (0,255,0)) #sucesso elif ( ida < 10 and volta < 10 and tamanhoCompativel == True): imgResultado = contorna(imgResultado, cnts2[idx2], (0,255,0)) #sucesso else: imgResultado = contorna(imgResultado, cnts2[idx2], (0,0,255)) #falha resultadoApi = False pathTxt = utils.buildPath(identificador, path="calc.txt") with open(pathTxt, "w") as text_file: text_file.write(sizeOut) text_file.write('\n') text_file.write(out) utils.save(names.RESULTADO, imgResultado, id=identificador) return resultadoApi
def train(self): extra_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(extra_update_ops): g_optim = tf.train.AdamOptimizer(self.flags.lr, beta1 = self.flags.beta1) \ .minimize(self.g_loss, var_list = self.g_vars) e_optim = tf.train.AdamOptimizer(self.flags.lr, beta1 = self.flags.beta1) \ .minimize(self.e_loss, var_list = self.e_vars) # d1_optim = tf.train.AdamOptimizer(self.flags.lr, beta1 = self.flags.beta1) \ # .minimize(self.d1_loss, var_list = self.d1_vars) # d2_optim = tf.train.AdamOptimizer(self.flags.lr, beta1 = self.flags.beta1) \ # .minimize(self.d2_loss, var_list = self.d2_vars) d1_optim = tf.train.RMSPropOptimizer(self.flags.lr).minimize( self.d1_loss, var_list=self.d1_vars) d2_optim = tf.train.RMSPropOptimizer(self.flags.lr).minimize( self.d2_loss, var_list=self.d2_vars) tf.global_variables_initializer().run() # merge summary # sum_total = tf.summary.merge([self.im_sum, self.im_hat_sum, self.recon_im_sum, # self.g_loss_sum, self.KL_fake_g_loss_sum, self.d1_g_loss_sum, self.d2_im_hat_g_loss_sum, # self.e_loss_sum,self.KL_fake_e_loss_sum, self.KL_real_e_loss_sum, self.d2_z_hat_e_loss_sum, # self.d1_loss_sum, self.d2_loss_sum # ]) sum_total = tf.summary.merge([ self.imh_sum, self.imh_hat_sum, self.g_loss_sum, self.d1_g_loss_sum, self.d2_imh_hat_g_loss_sum, self.e_loss_sum, self.d2_iml_hat_e_loss_sum, self.d1_loss_sum, self.d2_loss_sum ]) writer = tf.summary.FileWriter( "%s/sr_GAN_log_%s" % (self.flags.checkpoint_dir, self.flags.dataset_name), self.sess.graph) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) could_load, checkpoint_counter = load(self.saver, self.sess, self.flags) if could_load: counter = checkpoint_counter print(" [*] Load SUCCESS") else: counter = 0 print(" [!] Load failed...") for i in xrange(counter, self.flags.iter): i += 1 _, _, d1_loss_, d2_loss_ = self.sess.run( [d1_optim, d2_optim, self.d1_loss, self.d2_loss]) for iter_e in range(self.updates['e']['num_updates']): if iter_e < (self.updates['e']['num_updates'] - 1): self.sess.run([e_optim]) else: # run with loss _, e_loss_ = self.sess.run([e_optim, self.e_loss]) for iter_g in range(self.updates['g']['num_updates']): if iter_g < (self.updates['g']['num_updates'] - 1): self.sess.run([g_optim]) else: # run with summary and loss _, sum_total_, g_loss_, = self.sess.run( [g_optim, sum_total, self.g_loss]) if np.mod(i, 10) == 0: writer.add_summary(sum_total_, i) print("iteration: [%2d], g_loss: %.8f, e_loss: %.8f, d1_loss: %.8f, d2_loss: %.8f" \ % (i, g_loss_, e_loss_, d1_loss_, d2_loss_ )) print('**************************') if np.mod(i, self.flags.save_iter) == 0 or i == self.flags.iter: # try to sample and save model [low_im, high_im, fakehigh_im ] = self.sess.run([self.iml, self.imh, self.imh_hat]) save_images(self.flags, low_im, i, 'iml') save_images(self.flags, high_im, i, 'imh') save_images(self.flags, fakehigh_im, i, 'imh_hat') save(self.saver, self.sess, self.flags, i) print('saved once ...')
def main(args): args.cuda = torch.cuda.is_available() os.makedirs(args.save_path, exist_ok=True) ut.logger(args) tb_sw = SummaryWriter(log_dir=args.tensorboard_dir) wandb.init(project='ghkg', sync_tensorboard=True, dir=args.wandb_dir) e2id = ut.index('entities.dict', args) r2id = ut.index('relations.dict', args) args.nentity = len(e2id) args.nrelation = len(r2id) for k, v in sorted(vars(args).items()): logging.info(f'{k} = {v}') tr_q = ut.read(os.path.join(args.dataset, 'train.txt'), e2id, r2id) vd_q = ut.read(os.path.join(args.dataset, 'valid.txt'), e2id, r2id) ts_q = ut.read(os.path.join(args.dataset, 'test.txt'), e2id, r2id) logging.info(f'# Train = {len(tr_q)}') logging.info(f'# Valid = {len(vd_q)}') logging.info(f'# Test = {len(ts_q)}') al_q = tr_q + vd_q + ts_q tp_ix, tp_rix = ut.type_index(args) if args.negative_type_sampling or args.type_evaluation else (None, None) e_ix, u_ix = ut.users_index(args) if args.heuristic_evaluation else (None, None) mdl = nn.DataParallel(KGEModel(args)) if args.cuda: mdl = mdl.cuda() logging.info('Model Parameter Configuration:') for name, param in mdl.named_parameters(): logging.info(f'Parameter {name}: {param.size()}') ev_ix = ut.event_index(tr_q) if args.do_train: tr_dl_s = DataLoader(TrainDataset(tr_q, tp_ix, tp_rix, ev_ix, 's', args), batch_size=args.batch_size, shuffle=True, num_workers=max(1, os.cpu_count() // 2)) tr_dl_o = DataLoader(TrainDataset(tr_q, tp_ix, tp_rix, ev_ix, 'o', args), batch_size=args.batch_size, shuffle=True, num_workers=max(1, os.cpu_count() // 2)) tr_it = BidirectionalOneShotIterator(tr_dl_s, tr_dl_o) lr = args.learning_rate wd = args.weight_decay opt = torch.optim.Adam(filter(lambda p: p.requires_grad, mdl.parameters()), lr=lr, weight_decay=wd) opt_sc = MultiStepLR(opt, milestones=list(map(int, args.learning_rate_steps.split(',')))) if args.checkpoint != '': logging.info(f'Loading checkpoint {args.checkpoint} ...') chk = torch.load(os.path.join(args.checkpoint, 'checkpoint.chk')) init_stp = chk['step'] mdl.load_state_dict(chk['mdl_state_dict']) if args.do_train: lr = chk['opt_state_dict']['param_groups'][0]['lr'] opt.load_state_dict(chk['opt_state_dict']) opt_sc.load_state_dict(chk['opt_sc_state_dict']) else: logging.info('Randomly Initializing ...') init_stp = 1 stp = init_stp logging.info('Start Training ...') logging.info(f'init_stp = {init_stp}') if args.do_train: logging.info(f'learning_rate = {lr}') logs = [] bst_mtrs = {} for stp in range(init_stp, args.max_steps + 1): log = train_step(mdl, opt, opt_sc, tr_it, args) logs.append(log) if stp % args.log_steps == 0: mtrs = {} for mtr in logs[0].keys(): mtrs[mtr] = sum([log[mtr] for log in logs]) / len(logs) ut.log('Training average', stp, mtrs) logs.clear() ut.tensorboard_scalars(tb_sw, 'train', stp, log) if args.do_valid and stp % args.valid_steps == 0: logging.info('Evaluating on Valid Dataset ...') mtrs = test_step(mdl, vd_q, al_q, ev_ix, tp_ix, tp_rix, e_ix, u_ix, args) if bst_mtrs.get(args.metric, None) is None or mtrs[args.metric] > bst_mtrs[args.metric]: bst_mtrs = mtrs.copy() var_ls = {'step': stp} ut.save(mdl, opt, opt_sc, var_ls, args) ut.log('Valid', stp, mtrs) ut.tensorboard_scalars(tb_sw, 'valid', stp, mtrs) ut.tensorboard_hparam(tb_sw, bst_mtrs, args) if args.do_eval: logging.info('Evaluating on Training Dataset ...') mtrs = test_step(mdl, tr_q, al_q, ev_ix, tp_ix, tp_rix, e_ix, u_ix, args) ut.log('Test', stp, mtrs) ut.tensorboard_scalars(tb_sw, 'eval', stp, mtrs) if args.do_test: valid_approximation, args.valid_approximation = args.valid_approximation, 0 test_log_steps, args.test_log_steps = args.test_log_steps, 100 logging.info('Evaluating on Test Dataset ...') mdl.load_state_dict(torch.load(os.path.join(args.save_path, f'checkpoint.chk'))['mdl_state_dict']) mtrs = test_step(mdl, ts_q, al_q, ev_ix, tp_ix, tp_rix, e_ix, u_ix, args) ut.log('Test', stp, mtrs) ut.tensorboard_scalars(tb_sw, 'test', stp, mtrs) args.valid_approximation = valid_approximation args.test_log_steps = test_log_steps tb_sw.flush() tb_sw.close()
def extrai(path, pathCnh, identificador): paramsDb = db.select() valorAceitavel = paramsDb[1] valorAceitavelCnh = paramsDb[1] whTolerancia = paramsDb[2] pxWhiteTolerancia = paramsDb[3] paramsOut = """ PARAMETROS Tolerancia Pontos: {0} Tolerancia Pontos CNH: {1} Variacao no tamanho: {2}% Tolerancia densidade: {3}%\n """.format(valorAceitavel, valorAceitavelCnh, whTolerancia, pxWhiteTolerancia) densidadeOut = "" cnhColor = cv2.imread(pathCnh, -1) existeCnh = (cnhColor is not None) if (existeCnh == True): print("Existe") color = cv2.imread(path, -1) color = cv2.resize(color, (0, 0), fx=0.3, fy=0.3) imgOriginal = color.copy() color = utils.removeSombras(color) utils.save('semSombra.jpg', color, id=identificador) imgGray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY) imgPbOriginal = imgGray.copy() utils.save('pb1.jpg', imgGray, id=identificador) imgGray, contours, hierarchy = extraiContornos(imgGray, identificador) utils.save('thr.jpg', imgGray, id=identificador) cnts2 = sorted(contours, key=sortAltura, reverse=True) assinaturas = list() for i, c in enumerate(cnts2): x, y, w, h = cv2.boundingRect(c) existeEntre = existeEntreAlgumaFaixa(assinaturas, y, h) if existeEntre == False: assinaturas.append((y - 5, y + h + 5)) imgCopy = imgOriginal.copy() larguraImg = imgOriginal.shape[1] for ass in assinaturas: cv2.rectangle(imgCopy, (50, ass[0]), (larguraImg - 50, ass[1]), (255, 0, 0), 2) utils.save('identificadas_ass.jpg', imgCopy, id=identificador) if len(assinaturas) != 5: msgEx = "Numero de assinaturas encontradas ({}) é diferente do esperado (5)".format( len(assinaturas)) raise QtdeAssinaturasException(msgEx, identificador) assinaturas = sorted(assinaturas) lista = dict() #ratioDilatacao = recuperaRatioDilatacao(cnts2, imgPbOriginal, identificador) for i, ass in enumerate(assinaturas): roi = imgPbOriginal[ass[0]:ass[1], 0:larguraImg] utils.save('roi_{}.jpg'.format(i), roi, id=identificador) #roi = utils.resize(roi, width=300, height=300) resized = roi.copy() #resized = cv2.blur(resized, (blurI,blurI)) #utils.save('__{}_blur1.jpg'.format(i), resized) #resized = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) #resized = cv2.blur(resized, (5,5)) retval, resized = cv2.threshold(resized, 120, 255, type=cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) utils.save('th_roi_{}.jpg'.format(i), resized, id=identificador) resized, densidade = utils.removeContornosPqnosImg(resized) utils.save('t_{}.jpg'.format(i), resized, id=identificador) #cv2.waitKey(0) #print('ratioDilatacao ' + str(ratioDilatacao)) #resized = utils.dilatation(resized, ratio=0.4) utils.save('t1_{}.jpg'.format(i), resized, id=identificador) im2, contours2, hierarchy = cv2.findContours(resized, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours2, resized = utils.ajustaEspacosContorno(contours2, resized) cnts = sorted(contours2, key=functionSort, reverse=True)[0] novaMat = np.zeros(roi.shape, dtype="uint8") cv2.drawContours(novaMat, [cnts], -1, 255, -1) xA, yA, wA, hA = cv2.boundingRect(cnts) square = novaMat[yA:yA + hA, xA:xA + wA] utils.save('square_{}.jpg'.format(i), square, id=identificador) #moment = mahotas.features.zernike_moments(square, 21) densidadeOut += "Densidade {} = {}\n".format(i, densidade) lista[i] = cnts, ass, square, densidade #utils.show(color) hd = cv2.createHausdorffDistanceExtractor() sd = cv2.createShapeContextDistanceExtractor() out = "" outCnh = "" sizeOut = "" resultadoApi = True imgResultado = imgOriginal.copy() percentCnh = [] for idx1 in range( 0, 1): #recupera apenas a primeira imagem e a compara com as outras item1 = lista[idx1][0] square1 = lista[idx1][2] dens1 = lista[idx1][3] altura1, largura1 = calculaAlturaLargura(item1) soma = 0 item1 = transformaItem(square1, altura1, largura1, identificador, idx1) itemCnh = None if (existeCnh == True): itemCnh, squareCnh = cnh.validaAssinaturaCnh( cnhColor, square1, identificador) itemCnh = transformaItem(squareCnh, altura1, largura1, identificador, 6) print("Contornos img_6 = " + str(len(itemCnh))) for idx2 in range(0, 5): print("Processando imagem " + str(idx2)) item2 = lista[idx2][0] ass = lista[idx2][1] square2 = lista[idx2][2] dens2 = lista[idx2][3] altura2, largura2 = calculaAlturaLargura(item2) sizeOut += 'Dimensao {} x {} \n'.format(largura2, altura2) tamanhoCompativel = alturaLarguraCompativel( altura1, largura1, altura2, largura2, whTolerancia) densidadeCompativel = calcDensidadeCompativel( dens1, dens2, pxWhiteTolerancia) item2 = transformaItem(square2, altura1, largura1, identificador, idx2) print("Contornos img_" + str(idx2) + " = " + str(len(item2))) #match = hd.computeDistance(item1, item2) if (idx1 != idx2): idaSD = round(sd.computeDistance(item1, item2), 5) voltaSD = round(sd.computeDistance(item2, item1), 5) idaHD = round(hd.computeDistance(item1, item2), 5) voltaHD = round(hd.computeDistance(item2, item1), 5) idaMM = calculaMoment(item1, idx1, item2, idx2, identificador) voltaMM = idaMM else: idaSD = 0 voltaSD = 0 idaHD = 0 voltaHD = 0 idaMM = 0 voltaMM = 0 if (existeCnh == True): idaCnh = round(sd.computeDistance(item2, itemCnh), 5) voltaCnh = round(sd.computeDistance(itemCnh, item2), 5) percentSimCnh = calculaSimilaridade(idaCnh, voltaCnh, valorAceitavelCnh) outCnh += '{} == {} - {} = {}%\n'.format( idx2, idaCnh, voltaCnh, percentSimCnh) percentCnh.append(percentSimCnh) #ida = dist.euclidean(item1, item2) #volta = dist.euclidean(item2, item1) out += '{} vs {} (T{}, D{}) \n'.format(idx1, idx2, tamanhoCompativel, densidadeCompativel) out += '----SD: {} - {} \n'.format(idaSD, voltaSD) out += '----HD: {} - {} \n'.format(idaHD, voltaHD) out += '----MH: {} - {} \n'.format(idaMM, voltaMM) #BGR if (idaSD < valorAceitavel and voltaSD < valorAceitavel and tamanhoCompativel == True and densidadeCompativel == True): imgResultado = contorna(imgResultado, larguraImg, ass, (0, 255, 0)) #sucesso else: imgResultado = contorna(imgResultado, larguraImg, ass, (0, 0, 255)) #falha resultadoApi = False pathTxt = utils.buildPath(identificador, path="calc.txt") with open(pathTxt, "w") as text_file: text_file.write(paramsOut) text_file.write('\n') text_file.write(sizeOut) text_file.write('\n') text_file.write(densidadeOut) text_file.write('\n') text_file.write(out) text_file.write('\n') text_file.write(outCnh) utils.save(names.RESULTADO, imgResultado, id=identificador) return { 'folhaAssinatura': resultadoApi, 'resultadoCnh': False, 'percentCnh': percentCnh }
train_ds, test_ds, user_groups = get_dataset(args) # build users models models = initialize_models(args, same=True) # set up the network topology topology = random_graph(models, rho=0.3) # include physical edge devices (count < 1 to only use simulated nodes) edge = edge_devices(args, count=-1) # build the network graph graph = network_graph(topology, models, train_ds, test_ds, user_groups, args, edge=edge) graph.show_neighbors() # graph.show_similarity(ids=False) # Phase I: Local Training train_logs = graph.local_training(inference=True) # Phase II: Collaborative training collab_logs = graph.collaborative_training(learner=mp, args=args) info = {'xlabel': "Rounds", 'title': "Accuracy. vs. No. of rounds"} logs = { pid: train_logs[pid] + collab_logs[pid] for pid in train_logs.keys() } save(f"mp_log_{args.num_users}_{args.epochs}", logs) plot_train_history(logs, metric='accuracy', measure="mean-std") print("END.")
def get_data(args, phase): dataset = args['dataset'] seq_len = args['data_seq_len'] file_folder = args['data_folder'] aligned = args['aligned'] zsl = args['zsl'] fsl = args['fsl'] if phase == 'train' else -1 processed_path = f'./processed_datasets/{dataset}_{seq_len}_{phase}{"" if aligned else "_noalign"}.pt' if os.path.exists(processed_path) and zsl == -1 and fsl == -1: print(f'Load processed dataset! - {phase}') return load(processed_path) if dataset == 'mosi': if seq_len == 20: data_path = os.path.join(file_folder, f'X_{phase}.h5') label_path = os.path.join(file_folder, f'y_{phase}.h5') data = np.array(h5py.File(data_path, 'r')['data']) labels = np.array( h5py.File(label_path.replace('X', 'y'), 'r')['data']) text = data[:, :, :300] audio = data[:, :, 300:305] vision = data[:, :, 305:] this_dataset = MOSI(list(range(len(labels))), text, audio, vision, labels, is20=True) else: data_path = os.path.join( file_folder, f'mosi_data{"" if aligned else "_noalign"}.pkl') data = load(data_path) data = data[phase] this_dataset = MOSI(data['id'], data['text'], data['audio'], data['vision'], data['labels']) # elif dataset == 'mosei_senti': # if seq_len == 20: # text_data = np.array(h5py.File(os.path.join(file_folder, f'text_{phase}.h5'), 'r')['d1']) # audio_data = np.array(h5py.File(os.path.join(file_folder, f'audio_{phase}.h5'), 'r')['d1']) # vision_data = np.array(h5py.File(os.path.join(file_folder, f'vision_{phase}.h5'), 'r')['d1']) # labels = np.array(h5py.File(os.path.join(file_folder, f'y_{phase}.h5'), 'r')['d1']) # this_dataset = MOSEI(list(range(len(labels))), text_data, audio_data, vision_data, labels) # else: # data_path = os.path.join(file_folder, f'mosei_senti_data{"" if aligned else "_noalign"}.pkl') # data = load(data_path) # data = data[phase] # this_dataset = MOSEI(data['id'], data['text'], data['audio'], data['vision'], data['labels']) elif dataset == 'mosei_emo': text_data = np.array( h5py.File(os.path.join(file_folder, f'text_{phase}_emb.h5'), 'r')['d1']) audio_data = np.array( h5py.File(os.path.join(file_folder, f'audio_{phase}.h5'), 'r')['d1']) vision_data = np.array( h5py.File(os.path.join(file_folder, f'video_{phase}.h5'), 'r')['d1']) labels = np.array( h5py.File(os.path.join(file_folder, f'ey_{phase}.h5'), 'r')['d1']) # (N, 6) # Class order: Anger Disgust Fear Happy Sad Surprise labels = np.array(labels > 0, np.int32) this_dataset = MOSEI(list(range(len(labels))), text_data, audio_data, vision_data, labels, zsl=zsl, fsl=fsl) elif dataset == 'iemocap': data_path = os.path.join( file_folder, f'iemocap_data{"" if aligned else "_noalign"}.pkl') data = load(data_path) data = data[phase] # iemocap4 Distribution # neutral happy sad angry # [954 338 690 735] # [358 116 188 136] # [383 135 193 227] text_data = data['text'] audio_data = data['audio'] vision_data = data['vision'] labels = data['labels'] labels = np.argmax(labels, axis=-1) if zsl != -1: # iemocap9 Distribution # 0 1 2 3 4 5 6 7 8 # Anger Excited Fear Sad Surprised Frustrated Happy Neutral Disgust # [735 686 19 690 65 1235 338 954 1] (Train) # [136 206 9 188 17 319 116 358 0] (Valid) # [227 141 12 193 25 278 135 383 1] (Test) iemocap9_text_data = load2( os.path.join(file_folder, f'text_{phase}.p')) iemocap9_audio_data = load2( os.path.join(file_folder, f'covarep_{phase}.p')) iemocap9_vision_data = load2( os.path.join(file_folder, f'facet_{phase}.p')) iemocap9_labels = load2(os.path.join(file_folder, f'y_{phase}.p')) iemocap9_labels = iemocap9_labels[:, 1:-1] iemocap9_labels = np.expand_dims(iemocap9_labels[:, zsl], axis=1) nonzeros = [ i for i, l in enumerate(iemocap9_labels) if np.sum(l) != 0 ] zsl_text_data = iemocap9_text_data[nonzeros] zsl_audio_data = iemocap9_audio_data[nonzeros] zsl_vision_data = iemocap9_vision_data[nonzeros] zsl_labels = iemocap9_labels[nonzeros] # Align seq len to 20 zsl_text_data = zsl_text_data[:, :-1, :] zsl_audio_data = zsl_audio_data[:, :-1, :] zsl_vision_data = zsl_vision_data[:, :-1, :] text_data = np.concatenate((text_data, zsl_text_data), axis=0) audio_data = np.concatenate((audio_data, zsl_audio_data), axis=0) vision_data = np.concatenate((vision_data, zsl_vision_data), axis=0) labels = np.concatenate((labels, np.zeros((len(labels), 1))), axis=1) zsl_labels = np.concatenate((np.zeros( (len(zsl_labels), 4)), zsl_labels), axis=1) labels = np.concatenate((labels, zsl_labels), axis=0) this_dataset = IEMOCAP(list(range(len(labels))), text_data, audio_data, vision_data, labels) else: raise ValueError('Wrong dataset!') if zsl == -1 and fsl == -1: save(this_dataset, processed_path) return this_dataset
# print experiment details exp_details(args) # load dataset and initialize user groups train_ds, test_ds, user_groups = get_dataset(args) # build users models models = initialize_models(args, same=True) # set up the network topology || 10 (sigma=0.4) // 100 (sigma=0.9) // 300 (sigma=0.95) topology = random_graph(models, rho=0.3) # 1, 0.6, 0.3, 0.05, 0.01 # include physical edge devices (count < 1 to only use simulated nodes) edge = edge_devices(args, count=-1) # build the network graph graph = network_graph(topology, models, train_ds, test_ds, user_groups, args, edge=edge) graph.show_neighbors() # graph.show_similarity(ids=False) # Phase I: Local Training graph.local_training(inference=False) # Phase II: Collaborative training train_logs = graph.collaborative_training(learner=sp3, args=args) save(f"p3_log_{args.num_users}_{args.epochs}", train_logs) info = {'xlabel': "Rounds", 'title': "Accuracy. vs. No. of rounds"} plot_train_history(train_logs, metric='accuracy', measure="mean-std") print("END.")
def save_model(self): save( self.best_model, os.path.join(self.saving_path, 'models', self.get_saving_file_name()))
def save(self): utils.save( self.model, os.path.join(self.args.save, 'weights_{}.pt'.format(self.args.task_id)))