def __init__(self, _join_txid, _join_block, _settled_block, _bet_round, _bet_level, _bet_number, _bet_amount, _bet_reward, _bet_state, _player_address, _join_time, _payment_txid): self.txid = _join_txid self.short_txid = _join_txid[:10] + "..." self.join_block = _join_block self.settled_block = _settled_block self.short_txid_link_to = "" self.bet_round = _bet_round self.bet_level = _bet_level self.bet_number = _bet_number self.bet_amount = util.get_precision(_bet_amount, 2) self.bet_reward = util.get_precision(_bet_reward, 2) self.bet_state = _bet_state self.player_address = _player_address self.join_time = _join_time self.payment_txid = _payment_txid if self.payment_txid == "": self.short_payment_txid = "-" else: self.short_payment_txid = self.payment_txid[:10] + "..." if self.bet_state == 1: self.bet_reward += self.bet_amount else: self.bet_reward = -self.bet_amount
def send_unspent_list_to_address(unspent_list, to_address): send_amount = 0 input_list = [] for unspent in unspent_list: input_txid = unspent['txid'] vout = unspent['vout'] amount = unspent['amount'] send_amount += amount a_input = {'"txid"': '"{}"'.format(input_txid), '"vout"': vout} input_list.append(a_input) fee = ((len(unspent_list) * 180 + 1 * 34 + 10 + 40) / 1024) * 0.000015 fee = util.get_precision(fee, 8) send_amount -= fee send_amount = util.get_precision(send_amount, 8) output_dict = {'"{}"'.format(to_address): send_amount} txid_json_str = json.dumps(input_list) output_json_str = json.dumps(output_dict) command = 'createrawtransaction "{}" "{}"'.format(txid_json_str, output_json_str) # create raw transaction result = do_command(command) if result == -1: print( "Send unspent list to address faild at Create Raw Transaction Command!" ) return -1 # sign raw transaction command = 'signrawtransaction "{}"'.format(result) result = do_command(command) if result == -1: print( "Send unspent list to address faild at Sign Raw Transaction Command!" ) return -1 result = json.loads(result) # send raw transaction command = 'sendrawtransaction "{}"'.format(result['hex']) result = do_command(command) if result != -1: print("Input count: {}".format(len(unspent_list))) print(result, send_amount, '->', to_address, "fee: {}".format(fee)) return result
def __init__(self, _db_bet_round): self.round = _db_bet_round.bet_round self.number = _db_bet_round.bet_number self.level = _db_bet_round.bet_level self.bet_count = _db_bet_round.bet_count self.bet_amount = util.get_precision(_db_bet_round.total_bet_amount, 2) self.winner_count = _db_bet_round.winner_count self.loser_count = _db_bet_round.loser_count self.reward = util.get_precision(_db_bet_round.total_reward, 2) self.start_bet_block = _db_bet_round.start_block self.end_bet_block = _db_bet_round.settled_block - 1 self.reward_block = _db_bet_round.settled_block self.nonce = _db_bet_round.block_nonce
def create_raw_transaction(unspent_list, send_dict, change_address=None): input_amount = 0 input_list = [] for unspent in unspent_list: input_txid = unspent['txid'] vout = unspent['vout'] amount = unspent['amount'] input_amount += amount a_input = {'"txid"': '"{}"'.format(input_txid), '"vout"': vout} input_list.append(a_input) fee = ((len(unspent_list) * 180 + (len(send_dict) + 1) * 34 + 10 + 40) / 1024) * 0.000015 fee = util.get_precision(fee, 8) print("Raw Fee: ", fee) out_dict = {} total_send_amount = 0 for address in send_dict: send_amount = send_dict[address] send_amount = util.get_precision(send_amount, 8) total_send_amount += send_amount key = '"{}"'.format(address) out_dict[key] = send_amount total_send_amount = util.get_precision(total_send_amount, 8) change_amount = input_amount - total_send_amount - fee change_amount = util.get_precision(change_amount, 8) print("Input Amount: ", input_amount, "Send Amount: ", total_send_amount, "Change Amount: ", change_amount, "fee: ", fee) if change_amount < 0: print("No enough money to send!") return -1 if change_address is None: key = '"{}"'.format(change_address) out_dict[key] = change_amount txid_json_str = json.dumps(input_list) output_json_str = json.dumps(out_dict) command = 'createrawtransaction "{}" "{}"'.format(txid_json_str, output_json_str) return do_command(command)
def init_recently_winners(self, _bet_level): bet_list = db.get_recently_winner_list(_bet_level, 7) for bet in bet_list: settledbet = SettledBet( bet.join_txid, bet.join_block_height, bet.settlement_block_height, bet.game_round, bet.bet_level, bet.bet_number, util.get_precision(bet.bet_amount, 2), util.get_precision(bet.reward_amount, 2), bet.bet_state, bet.payment_address, bet.created_at.strftime("%Y-%m-%d %H:%M:%S"), bet.reward_txid, ) self.recently_winners.append(settledbet.get_json_obj())
def get_account_balance(_account_name): total_balance = 0 unspent_list = get_unspent_list() for unspent in unspent_list: if unspent["account"] == _account_name: total_balance += unspent["amount"] total_balance = util.get_precision(total_balance, 8) return total_balance
def get_address_balance(_address): unspent_list = get_unspent_list_by_address(_address) total_balance = 0 if unspent_list is not None: for unspent in unspent_list: amount = unspent['amount'] total_balance += amount total_balance = util.get_precision(total_balance, 8) return total_balance
def step1_try_save_bet_list(_curr_block_height, _bet_level): bet_list = [] if _bet_level == 1: bet_address_dict = model.small_bet_address_dict bet_address_number_dict = model.small_address_number_dict elif _bet_level == 2: bet_address_dict = model.big_bet_address_dict bet_address_number_dict = model.big_address_number_dict elif _bet_level == 3: bet_address_dict = model.large_bet_address_dict bet_address_number_dict = model.large_address_number_dict else: print("Error!, Wrong Diff Level: {}".format(_bet_level)) return curr_game_round = db.get_curr_unsettle_game_round(_bet_level) for account_name in bet_address_dict: bet_address = bet_address_dict[account_name] unspent_list = api.get_unspent_list_by_address(bet_address) if len(unspent_list) > 0: for unspent in unspent_list: bet_data = {} bet_number = bet_address_number_dict.get(bet_address, None) if bet_number is None: print("Fatal Error, get bet number error! {}".format( bet_address)) return unspent_txid = unspent["txid"] join_block_height, join_block_hash, join_block_timestamp = \ api.get_block_height_hash_timestamp_by_txid(unspent["txid"]) input_address_list = api.get_input_addresses(unspent_txid) if len(input_address_list) == 0: print("Fatal Error, get input address faild! {}".format( unspent_txid)) return bet_data["join_txid"] = unspent_txid bet_data["join_block_height"] = join_block_height bet_data["join_block_hash"] = join_block_hash bet_data["join_block_timestamp"] = join_block_timestamp bet_data["bet_number"] = bet_number bet_data["bet_address"] = unspent["address"] bet_data["bet_amount"] = util.get_precision( float(unspent["amount"]), 8) bet_data["payment_address"] = input_address_list[0] bet_data["bet_level"] = _bet_level bet_data["bet_round"] = curr_game_round bet_list.append(bet_data) db.save_new_bet_list(bet_list, _bet_level)
def send_to_many_from_unspent_list(unspent_list, to_dict, change_address): if unspent_list is None or len(unspent_list) == 0: print("No unspent list!") return -1 if to_dict is None or len(to_dict) == 0: print("No to dict!") return -1 input_list = list() total_balance = 0.0 for unspent in unspent_list: txid = unspent['txid'] vout = unspent['vout'] amount = unspent['amount'] total_balance += util.get_precision(amount, 8) input_list.append({"txid": txid, "vout": vout}) total_send = 0.0 for key in to_dict: send_amount = to_dict[key] total_send += send_amount fee = ((len(unspent_list) * 180 + (len(to_dict) + 1) * 34 + 10 + 40) / 1024) * 0.000015 fee = util.get_precision(fee, 8) change_amount = total_balance - total_send - fee change_amount = util.get_precision(change_amount, 8) if change_amount < 0: print("No enough money to send, balance: {}, send: {}".format( total_balance, total_send)) return -1 to_dict[change_address] = change_amount transaction_hash = rpc.createrawtransaction(input_list, to_dict) signed_hex = rpc.signrawtransaction(transaction_hash)['hex'] out_txid = rpc.sendrawtransaction(signed_hex) print("Balance: {}, send: {}, fee: {}, change: {}, out_txid: {}".format( total_balance, total_send, fee, change_amount, out_txid)) return out_txid
def __init__(self, _join_txid, _join_block, _bet_round, _bet_level, _bet_number, _bet_amount, _player_address, _join_time): self.short_txid = _join_txid[:10] + "..." self.txid = _join_txid self.join_block = _join_block self.short_txid_link_to = "" self.bet_round = _bet_round self.bet_level = _bet_level self.bet_number = _bet_number self.bet_amount = util.get_precision(_bet_amount, 2) self.player_address = _player_address self.join_time = _join_time
def send_to_address_from_unspent_list(unspent_list, to_address): if unspent_list is None or len(unspent_list) == 0: print("No unspent list!") return -1 if to_address is None: print("No to address!") return -1 input_list = [] total_balance = 0.0 for unspent in unspent_list: txid = unspent['txid'] vout = unspent['vout'] amount = unspent['amount'] total_balance += util.get_precision(amount, 8) input_list.append({"txid": txid, "vout": vout}) fee = ((len(unspent_list) * 180 + 1 * 34 + 10 + 40) / 1024) * 0.000015 fee = util.get_precision(fee, 8) total_send = total_balance - fee total_send = util.get_precision(total_send, 8) send_dict = dict() send_dict[to_address] = total_send if total_send <= 0.0: print("No enough money, send faild!") return -1 transaction_hash = rpc.createrawtransaction(input_list, send_dict) signed_hex = rpc.signrawtransaction(transaction_hash)['hex'] out_txid = rpc.sendrawtransaction(signed_hex) print("Balance: {}, send: {}, fee: {}, out_txid: {}".format( total_balance, total_send, fee, out_txid)) return out_txid
def init_recently_unsettle_bets(self): bet_list = db.get_recently_unsettle_bet_list() for bet in bet_list: unsettlebet = UnSettleBet( bet.join_txid, bet.join_block_height, bet.game_round, bet.bet_level, bet.bet_number, util.get_precision(bet.bet_amount, 2), bet.payment_address, bet.join_block_timestamp, #bet.created_at.strftime("%Y-%m-%d %H:%M:%S") ) self.recently_unsettle_bets.append(unsettlebet.get_json_obj())
def step2_try_settle_bets(_curr_block_height, _bet_level): unsettle_bet_list = db.get_unsettle_bet_list(_bet_level) settled_game_round_list = [] if unsettle_bet_list is None or len(unsettle_bet_list) == 0: return settled_game_round_list min_bet_amount = model.get_min_bet_amount(_bet_level) min_block_height = -1 max_block_height = -1 bet_dict = {} for bet in unsettle_bet_list: log.Info("###############: {} {} {}".format(bet.join_txid, bet.join_block_hash, bet.bet_number)) if min_block_height < 0 or bet.join_block_height < min_block_height: min_block_height = bet.join_block_height if max_block_height < 0 or bet.join_block_height > max_block_height: max_block_height = bet.join_block_height bet_dict[bet.join_txid] = bet if _curr_block_height == min_block_height: return settled_game_round_list for block in range(min_block_height, _curr_block_height + 1): check_block = block + 1 if check_block > _curr_block_height: break block_hash, block_nonce, block_timestamp = api.get_block_hash_nonce_timestamp_by_height( check_block) nonce_last_number = int(str(block_nonce)[-1]) selected_bet_list = [] has_loser = False has_winer = False log.Info("Check Block: {} {} {}".format(block, "Nonce: ", block_nonce)) for txid in bet_dict: dbbet = bet_dict[txid] if dbbet.join_block_height < check_block: selected_bet_list.append(dbbet) if dbbet.bet_amount < min_bet_amount: has_loser = True else: if dbbet.bet_number == nonce_last_number: has_winer = True else: has_loser = True log.Info("Has winer: {} {} {}".format(has_winer, "Has Loaser: ", has_loser)) if has_winer and has_loser: winer_list = [] loser_list = [] total_loser_amount = 0.0 total_winer_amount = 0.0 for dbbet in selected_bet_list: if dbbet.bet_amount < min_bet_amount: loser_list.append(dbbet) total_loser_amount += dbbet.bet_amount dbbet.bet_state = 0 else: if dbbet.bet_number == nonce_last_number: winer_list.append(dbbet) total_winer_amount += dbbet.bet_amount dbbet.bet_state = 1 else: loser_list.append(dbbet) total_loser_amount += dbbet.bet_amount dbbet.bet_state = 0 dbbet.settlement_block_height = check_block dbbet.settlement_block_hash = block_hash dbbet.settlement_block_nonce = block_nonce del bet_dict[dbbet.join_txid] log.Info("Winer Count: {} {} {}".format(len(winer_list), "Loser Count: ", len(loser_list))) log.Info("Total winer amount: {} {} {}".format( total_winer_amount, "Total loser amount: ", total_loser_amount)) total_reward = total_loser_amount * (1 - dev_reward_percentage) total_reward = util.get_precision(total_reward, 8) dev_reward = total_loser_amount - total_reward log.Info("Total winer reward: {} {} {}".format( total_reward, "dev reward: ", dev_reward)) pay_input_txid_list = [] to_dict = {} # pay to winers for dbbet in winer_list: reward_percentage = dbbet.bet_amount / total_winer_amount reward_percentage = util.get_precision(reward_percentage, 3) reward_amount = total_reward * reward_percentage reward_amount = util.get_precision(reward_amount, 8) dbbet.reward_amount = reward_amount pay_amount = reward_amount + dbbet.bet_amount pay_amount = util.get_precision(pay_amount, 8) to_dict[dbbet.payment_address] = pay_amount pay_input_txid_list.append(dbbet.join_txid) log.Info( "Txid {} bet_amount: {} reward%: {} reward:{} payment:{}" .format(dbbet.join_txid, dbbet.bet_amount, reward_percentage, reward_amount, pay_amount)) for dbbet in loser_list: pay_input_txid_list.append(dbbet.join_txid) pay_reward_txid = api.send_to_many_from_input_txid_list( pay_input_txid_list, to_dict, model.dev_reward_address) if pay_reward_txid == -1: log.Info("Error! Payment Faild!") settled_bet_list = [] for dbbet in winer_list: dbbet.payment_state = 1 dbbet.reward_txid = pay_reward_txid dbbet.update_at = datetime.datetime.now() settled_bet_list.append(dbbet) log.Info( "Winner: {} join_height: {} address:{} bet_number: {}". format(dbbet.join_txid, dbbet.join_block_height, dbbet.payment_address, dbbet.bet_number)) for dbbet in loser_list: dbbet.update_at = datetime.datetime.now() settled_bet_list.append(dbbet) log.Info( "Loser: {} Join_height: {} address: {} bet_number: {} bet_amount: {}" .format(dbbet.join_txid, dbbet.join_block_height, dbbet.payment_address, dbbet.bet_number, dbbet.bet_amount)) # last_game_round = db.get_last_game_round_number(_bet_level) curr_game_round = db.get_curr_unsettle_game_round(_bet_level) settled_game_round_list.append(curr_game_round) min_join_block = -1 max_join_block = -1 for dbbet in settled_bet_list: dbbet.game_round = curr_game_round if min_join_block < 0 or dbbet.join_block_height < min_join_block: min_join_block = dbbet.join_block_height if max_join_block < 0 or dbbet.join_block_height > max_join_block: max_join_block = dbbet.join_block_height db.save_settlement_bet_list(settled_bet_list) db.save_settled_header_data( curr_game_round, nonce_last_number, _bet_level, len(settled_bet_list), len(winer_list), len(loser_list), total_winer_amount + total_loser_amount, total_loser_amount, dev_reward, min_join_block, check_block, block_nonce, ) return settled_game_round_list
def get_json_obj(self): return { "round": self.round, "bet_count": self.bet_count, "bet_amount": util.get_precision(self.bet_amount, 2), }
print("best models in order of genres: ", best_models) print("best models accuracy in same order: ", best_models_accuracy) print("best models confusion matrix in same order: ", best_models_conf_mat) # Make a union of all classifiers outputs for each songs TP = 0 TN = 0 FP = 0 FN = 0 for track_index in range(len(x_test)): x_track = x_test[track_index] y_track = y_test[track_index] # For each track, for each genre, count the right and wrong predictions for genre_index in range(len(BASE_GENRES)): pred = best_models[genre_index].predict([x_track]) if y_track[genre_index] == True and pred[0] == y_track[genre_index]: TP += 1 elif y_track[genre_index] == False and pred[0] == y_track[genre_index]: TN += 1 elif pred[0] == True: FP += 1 else: FN += 1 print("\nConfusion Matrix") print(TP, TN) print(FP, FN) print("\nPrecision and F1 metric: ") print(get_precision(TP, FP), get_f1(TP, FP, FN)) plt.show()
def train_val(config): device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') train_loader = get_dataloader(img_dir=config.train_img_dir, mask_dir=config.train_mask_dir, mode="train", batch_size=config.batch_size, num_workers=config.num_workers) val_loader = get_dataloader(img_dir=config.val_img_dir, mask_dir=config.val_mask_dir, mode="val", batch_size=config.batch_size, num_workers=config.num_workers) writer = SummaryWriter( comment="LR_%f_BS_%d_MODEL_%s_DATA_%s" % (config.lr, config.batch_size, config.model_type, config.data_type)) if config.model_type not in [ 'UNet', 'R2UNet', 'AUNet', 'R2AUNet', 'SEUNet', 'SEUNet++', 'UNet++', 'DAUNet', 'DANet', 'AUNetR', 'RendDANet', "BASNet" ]: print('ERROR!! model_type should be selected in supported models') print('Choose model %s' % config.model_type) return if config.model_type == "UNet": model = UNet() elif config.model_type == "AUNet": model = AUNet() elif config.model_type == "R2UNet": model = R2UNet() elif config.model_type == "SEUNet": model = SEUNet(useCSE=False, useSSE=False, useCSSE=True) elif config.model_type == "UNet++": model = UNetPP() elif config.model_type == "DANet": model = DANet(backbone='resnet101', nclass=1) elif config.model_type == "AUNetR": model = AUNet_R16(n_classes=1, learned_bilinear=True) elif config.model_type == "RendDANet": model = RendDANet(backbone='resnet101', nclass=1) elif config.model_type == "BASNet": model = BASNet(n_channels=3, n_classes=1) else: model = UNet() if torch.cuda.device_count() > 1: print("Let's use", torch.cuda.device_count(), "GPUs!") model = nn.DataParallel(model) model = model.to(device, dtype=torch.float) if config.optimizer == "sgd": optimizer = SGD(model.parameters(), lr=config.lr, weight_decay=1e-6, momentum=0.9) else: optimizer = torch.optim.Adam(model.parameters(), lr=config.lr) if config.loss == "dice": criterion = DiceLoss() elif config.loss == "bce": criterion = nn.BCELoss() elif config.loss == "bas": criterion = BasLoss() else: criterion = MixLoss() scheduler = lr_scheduler.StepLR(optimizer, step_size=40, gamma=0.1) global_step = 0 best_dice = 0.0 for epoch in range(config.num_epochs): epoch_loss = 0.0 with tqdm(total=config.num_train, desc="Epoch %d / %d" % (epoch + 1, config.num_epochs), unit='img') as train_pbar: model.train() for image, mask in train_loader: image = image.to(device, dtype=torch.float) mask = mask.to(device, dtype=torch.float) d0, d1, d2, d3, d4, d5, d6, d7 = model(image) loss = criterion(d0, d1, d2, d3, d4, d5, d6, d7, mask) epoch_loss += loss.item() writer.add_scalar('Loss/train', loss.item(), global_step) train_pbar.set_postfix(**{'loss (batch)': loss.item()}) optimizer.zero_grad() loss.backward() optimizer.step() train_pbar.update(image.shape[0]) global_step += 1 # if global_step % 100 == 0: # writer.add_images('masks/true', mask, global_step) # writer.add_images('masks/pred', d0 > 0.5, global_step) scheduler.step() epoch_dice = 0.0 epoch_acc = 0.0 epoch_sen = 0.0 epoch_spe = 0.0 epoch_pre = 0.0 current_num = 0 with tqdm(total=config.num_val, desc="Epoch %d / %d validation round" % (epoch + 1, config.num_epochs), unit='img') as val_pbar: model.eval() locker = 0 for image, mask in val_loader: current_num += image.shape[0] image = image.to(device, dtype=torch.float) mask = mask.to(device, dtype=torch.float) d0, d1, d2, d3, d4, d5, d6, d7 = model(image) batch_dice = dice_coeff(mask, d0).item() epoch_dice += batch_dice * image.shape[0] epoch_acc += get_accuracy(pred=d0, true=mask) * image.shape[0] epoch_sen += get_sensitivity(pred=d0, true=mask) * image.shape[0] epoch_spe += get_specificity(pred=d0, true=mask) * image.shape[0] epoch_pre += get_precision(pred=d0, true=mask) * image.shape[0] if locker == 200: writer.add_images('masks/true', mask, epoch + 1) writer.add_images('masks/pred', d0 > 0.5, epoch + 1) val_pbar.set_postfix(**{'dice (batch)': batch_dice}) val_pbar.update(image.shape[0]) locker += 1 epoch_dice /= float(current_num) epoch_acc /= float(current_num) epoch_sen /= float(current_num) epoch_spe /= float(current_num) epoch_pre /= float(current_num) epoch_f1 = get_F1(SE=epoch_sen, PR=epoch_pre) if epoch_dice > best_dice: best_dice = epoch_dice writer.add_scalar('Best Dice/test', best_dice, epoch + 1) torch.save( model, config.result_path + "/%s_%s_%d.pth" % (config.model_type, str(epoch_dice), epoch + 1)) logging.info('Validation Dice Coeff: {}'.format(epoch_dice)) print("epoch dice: " + str(epoch_dice)) writer.add_scalar('Dice/test', epoch_dice, epoch + 1) writer.add_scalar('Acc/test', epoch_acc, epoch + 1) writer.add_scalar('Sen/test', epoch_sen, epoch + 1) writer.add_scalar('Spe/test', epoch_spe, epoch + 1) writer.add_scalar('Pre/test', epoch_pre, epoch + 1) writer.add_scalar('F1/test', epoch_f1, epoch + 1) writer.close() print("Training finished")
def get_f1(tp, fp, fn): prec = get_precision(tp, fp) tpr = get_tpr(tp, fn) return 2 * prec * tpr / (prec + tpr)
print("BlockHeight: ", curr_block_height) print("Nonce Count: ", len(nonce_list)) """ """ 游戏玩法: 1. 猜大小 """ nonce_list = [] last_digit_dict = {} for x in range(10): last_digit_dict[x] = 0 with open("nonces.txt", "r") as f: for snonce in f: snonce = snonce.strip() nonce_list.append(snonce) for nonce in nonce_list: last_digit = int(nonce[-1]) last_digit_dict[last_digit] += 1 nonce_count = len(nonce_list) for ld in last_digit_dict: count = last_digit_dict[ld] percentage = util.get_precision((count / nonce_count) * 100, 2) print(ld, count, percentage)