def __init__(self, logname, withuuid=True): print 'Init Log System...' if not os.path.exists('log'): os.mkdir('log') # 创建一个self.logger if withuuid: logname = logname + '_' + str(uuid.uuid1()) self.logger = logging.getLogger(logname) self.logger.setLevel(logging.INFO) # 创建一个handler,用于写入日志文件 filename = 'log/%s_%s.log' % (logname, utils.GetCurrentTime()) fh = logging.FileHandler(filename) fh.setLevel(logging.INFO) # 再创建一个handler,用于输出到控制台 ch = logging.StreamHandler() ch.setLevel(logging.INFO) # 定义handler的输出格式 #'%(asctime)s-%(name)s-%(levelname)s-%(message)s' formatter = logging.Formatter('[%(asctime)s]%(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # 给self.logger添加handler self.logger.addHandler(fh) self.logger.addHandler(ch)
def incChalNrTimes(userName, chalID, ip): with lock: try: dataBase = TinyDB(dbFileName) userTable = dataBase.table('chal_nr_times') row = { "userName": userName, "chalID": chalID, "ts": utils.GetCurrentTime() } userTable.insert(row) except Exception as e: print("Exception - incChalNrTimes: " + str(e) + "\n")
def addHeartBeat(userName, chalID, ip): with lock: try: dataBase = TinyDB(dbFileName) tLTable = dataBase.table('heartbeat') row = { "userName": userName, "chalID": chalID, "ts": utils.GetCurrentTime(), "ip": ip } tLTable.insert(row) except Exception as e: print("Exception - addHeartBeat: " + str(e) + "\n")
def addHintTag(userName, chalID, hintTag, html, ip): with lock: try: dataBase = TinyDB(dbFileName) tbl = dataBase.table('hints') row = { "userName": userName, "chalID": chalID, "ts": utils.GetCurrentTime(), "hint": hintTag, "html": html, "ip": ip } tbl.insert(row) except Exception as e: print("Exception - addHintTag: " + str(e) + "\n")
def addChalFeedback(userName, chalID, rate1, rate2, rate3, ip): with lock: try: dataBase = TinyDB(dbFileName) tbl = dataBase.table('chal_feedback') row = { "userName": userName, "chalID": chalID, "ts": utils.GetCurrentTime(), "rate1": str(rate1), "rate2": str(rate2), "rate3": str(rate3), "ip": ip } tbl.insert(row) except Exception as e: print("Exception - addChalFeedback: " + str(e) + "\n")
def addInteraction(userName, chalID, iType, message, folder, ip): with lock: try: dataBase = TinyDB(dbFileName) tLTable = dataBase.table('timeline') row = { "userName": userName, "chalID": chalID, "ts": utils.GetCurrentTime(), "type": iType, "message": message, "folder": folder, "ip": ip } tLTable.insert(row) except Exception as e: print("Exception - addInteraction: " + str(e) + "\n")
def unlockChallenge(userName, chalID, ip): with lock: try: dataBase = TinyDB(dbFileName) unlockTable = dataBase.table('unlock') q = Query() r = unlockTable.search((q.chalID == chalID) & (q.userName == userName)) if len(r) == 0: row = { "userName": userName, "chalID": chalID, "ts": utils.GetCurrentTime(), "ip": ip } unlockTable.insert(row) except Exception as e: print("Exception - unlockChallenge: " + str(e) + "\n")
def unlockFile(userName, chalID, fileName, unlockCode, ip): with lock: try: dataBase = TinyDB(dbFileName) tbl = dataBase.table('unlock_file') q = Query() r = tbl.search((q.chalID == chalID) & (q.userName == userName) & (q.unlockCode == unlockCode)) if len(r) == 0: row = { "userName": userName, "chalID": chalID, "fileName": fileName, "unlockCode": unlockCode, "ts": utils.GetCurrentTime(), "ip": ip } tbl.insert(row) except Exception as e: print("Exception - unlockFile: " + str(e) + "\n")
def insertReport(userName, chalID, message, userContent, inputContent, folder, ip): exception = None with lock: try: dataBase = TinyDB(dbFileName) repTable = dataBase.table('reports') report = { "userName": userName, "chalID": chalID, "message": message, "userContent": userContent, "inputContent": inputContent, "ts": utils.GetCurrentTime(), "folder": folder, "ip": ip } repTable.insert(report) except: exception = "could not open database" if (exception != None): raise Exception(exception)
def test(net, test_Loader, device, display_interval, out_dir, num_classes, iterations): print('{}: Start testing '.format(utils.GetCurrentTime())) confusionMatrix = np.zeros((num_classes, num_classes)) proba_all = [] label_all = [] # predict_proba = [] predict_class = [] start_time = time.time() with torch.no_grad(): for iter_val, data in enumerate(test_Loader): if iter_val % display_interval == (display_interval - 1): print("Iterations: [{}/{}]".format(iter_val + 1, iterations)) # images, labels = data images, labels = data[0].to(device), data[1] outputs = net(images) proba = utils.softmax(outputs.cpu()) predicted = torch.max(proba, 1) proba_all.extend(proba.numpy()) label_all.extend(labels.numpy()) # predict_proba.extend(predicted[0].numpy()) predict_class.extend(predicted[1].numpy()) for idx, label in enumerate(labels): confusionMatrix[label, predicted[1][idx]] += 1 print("Iterations: [{}/{}]".format(iterations, iterations)) end_time = time.time() print('{}: Testing Finished'.format(utils.GetCurrentTime())) data_size = len(predict_class) proba_all = np.array(proba_all) label_all = np.reshape(label_all, (-1, 1)) predict_class = np.reshape(predict_class, (-1, 1)) accuracy = np.sum(np.diag(confusionMatrix)) / data_size error = np.round(1 - accuracy, 6) accuracy_perClass = np.diag(confusionMatrix) / np.sum(confusionMatrix, axis=0) error_perClass = 1 - accuracy_perClass output = np.concatenate([proba_all, label_all, predict_class], axis=1) output = output.astype('float') probaMatrixPath = os.path.join(out_dir, "TestProbaMatrix.txt") confMatPath = os.path.join(out_dir, '{}_Err={}.txt'.format("TestConfMat", error)) np.savetxt(probaMatrixPath, output, fmt='%f') np.savetxt(confMatPath, confusionMatrix, fmt='%d', delimiter='\t') time_per_img = (end_time - start_time) * 1000 / data_size print("\n#------------------------------------------------------#\n") print("Overall Accuracy : {}%".format(round(accuracy * 100, 2))) print("Overall Error Rate : {}%".format(round(error * 100, 2))) print('Test time per image (ms): {}'.format(round(time_per_img, 4))) print() print("Dump probability matrix: {}".format( os.path.split(probaMatrixPath)[-1])) print("Dump confusion matrx: {}".format(os.path.split(confMatPath)[-1])) print('\n=============================') print('Error Rate (per-class): ') for l in range(num_classes): print(' class {} : {}%'.format(l, round(error_perClass[l] * 100, 2))) print('=============================\n')