コード例 #1
0
ファイル: Model.py プロジェクト: yanglei50/DeepFakeTutorial
    def save_weights(self):        
        model_dir = str(self.model_dir)
        
        try:
            for model in hdf.values():            
                backup_file(model_dir, model)
        except NameError:
            print('backup functionality not available\n')       
            
        state_dir = os.path.join(model_dir, 'state_{version_str}_{ENCODER.value}.json'.format(**globals()))
        ser = lib.Serializer.get_serializer('json')
        try:
            with open(state_dir, 'wb') as fp:
                state_json = ser.marshal({
                    'epoch_no' : self._epoch_no
                     })
                fp.write(state_json.encode('utf-8'))
        except IOError as e:
            print(e.strerror)                   
        
        print('\nsaving model weights', end='', flush=True)        
        
        from concurrent.futures import ThreadPoolExecutor, as_completed        
        
        with ThreadPoolExecutor(max_workers=4) as executor:
            futures = [executor.submit(getattr(self, mdl_name.rstrip('H5')).save_weights, str(self.model_dir / mdl_H5_fn)) for mdl_name, mdl_H5_fn in hdf.items()]
            for future in as_completed(futures):
                future.result()
                print('.', end='', flush=True)  

        print('done', flush=True)              
コード例 #2
0
    def save_weights(self):
        from threading import Thread
        from time import sleep

        model_dir = str(self.model_dir)

        try:
            for model in hdf.values():
                backup_file(model_dir, model)
        except NameError:
            print('backup functionality not available\n')

        # thought maybe I/O bound, sometimes saving in parallel is faster
        threads = []
        t = Thread(target=self.encoder.save_weights,
                   args=(str(self.model_dir / hdf['encoderH5']), ))
        threads.append(t)
        t = Thread(target=self.decoder_A.save_weights,
                   args=(str(self.model_dir / hdf['decoder_AH5']), ))
        threads.append(t)
        t = Thread(target=self.decoder_B.save_weights,
                   args=(str(self.model_dir / hdf['decoder_BH5']), ))
        threads.append(t)

        for thread in threads:
            thread.start()

        while any([t.is_alive() for t in threads]):
            sleep(0.1)

        print('saved model weights')
コード例 #3
0
ファイル: pge_bill_paid.py プロジェクト: rmeineke/welldev
def main():
    database = 'well.sqlite'
    logger = logbook.Logger('pge_bill_paid')

    utils.backup_file(database)
    db = sqlite3.connect(database)
    db.row_factory = sqlite3.Row
    cur = db.cursor()

    # get date_paid
    payment_date = utils.prompt_for_current_date("Date paid")
    # get amount
    payment_amount = utils.prompt_for_amount("Amount paid")
    const = constants.Constants()

    # mark the bill paid in the activity account
    payment_amount = payment_amount * -1
    exec_str = f"""
                INSERT INTO activity (date, type, amount, note) 
                VALUES (?, ?, ?, ?)
            """
    params = (payment_date, const.pge_bill_paid, payment_amount, "PGE bill paid")
    logger.trace(params)
    cur.execute(exec_str, params)

    # save, then close the cursor and db
    db.commit()
    cur.close()
    db.close()
コード例 #4
0
ファイル: AutoEncoder.py プロジェクト: zys0070/faceswap
 def save_weights(self):
     model_dir = str(self.model_dir)
     for model in hdf.values():
         backup_file(model_dir, model)
     self.encoder.save_weights(str(self.model_dir / hdf['encoderH5']))
     self.decoder_A.save_weights(str(self.model_dir / hdf['decoder_AH5']))
     self.decoder_B.save_weights(str(self.model_dir / hdf['decoder_BH5']))
     logger.info('saved model weights')
コード例 #5
0
ファイル: AutoEncoder.py プロジェクト: Nioy/faceswap
 def save_weights(self):
     model_dir = str(self.model_dir)
     for model in hdf.values():
         backup_file(model_dir, model)
     self.encoder.save_weights(str(self.model_dir / hdf['encoderH5']))
     self.decoder_A.save_weights(str(self.model_dir / hdf['decoder_AH5']))
     self.decoder_B.save_weights(str(self.model_dir / hdf['decoder_BH5']))
     print('saved model weights')
コード例 #6
0
def main():
    payment_logger = logbook.Logger('payment_recd')
    db = "well.sqlite"

    database = sqlite3.connect(f"{db}")
    cur = database.cursor()

    acct = utils.prompt_for_account("Please choose an account", cur)
    #
    # show the account's current balance here
    # fetch balance and display
    cur_balance = utils.get_acct_balance(acct, cur)
    if cur_balance == 0:
        print(f"This account has a zero balance.")
        exit(0)

    date = utils.prompt_for_current_date("Payment date")

    # fetch amount and flip the sign
    print(f"Current balance: ${cur_balance:.2f}")
    amt = utils.prompt_for_amount("Payment amount")
    payment_logger.debug(f"get_amount just returned this amount: {amt}")
    amt *= -1

    # cobble together the account note
    # changed this to be multi-line so the payment method can
    # a little longer .... 2019.06.21
    notes = "Payment on account|"
    notes += utils.prompt_for_notes("Payment notes")

    payment_logger.debug(date)
    payment_logger.debug(amt)
    payment_logger.debug(acct)
    payment_logger.debug(notes)

    # backup the file prior to adding any data
    utils.backup_file(db)

    const = constants.Constants()
    # insert the payment into the DB
    cur.execute(
        "INSERT INTO activity (date, acct, type, amount, note) VALUES (?, ?, ?, ?, ?)",
        (date, acct, const.payment_received, amt, notes),
    )

    # fetch updated balance and display
    cur_balance = utils.get_acct_balance(acct, cur)
    print(f"Updated balance: ${cur_balance:.2f}\n")

    # save, then close the cursor and database
    database.commit()
    cur.close()
    database.close()
コード例 #7
0
 def save_weights(self):
     model_dir = str(self.model_dir)
     for model in hdf.values():
         backup_file(model_dir, model)
     if self.gpus > 1:
         self.netGA_sm.save_weights(str(self.model_dir / hdf['netGAH5']))
         self.netGB_sm.save_weights(str(self.model_dir / hdf['netGBH5']))
     else:
         self.netGA.save_weights(str(self.model_dir / hdf['netGAH5']))
         self.netGB.save_weights(str(self.model_dir / hdf['netGBH5']))
     self.netDA.save_weights(str(self.model_dir / hdf['netDAH5']))
     self.netDB.save_weights(str(self.model_dir / hdf['netDBH5']))
     logger.info("Models saved.")
コード例 #8
0
ファイル: Model.py プロジェクト: Nioy/faceswap
 def save_weights(self):
     model_dir = str(self.model_dir)
     for model in hdf.values():
         backup_file(model_dir, model)
     if self.gpus > 1:
         self.netGA_sm.save_weights(str(self.model_dir / hdf['netGAH5']))
         self.netGB_sm.save_weights(str(self.model_dir / hdf['netGBH5']))
     else:
         self.netGA.save_weights(str(self.model_dir / hdf['netGAH5']))
         self.netGB.save_weights(str(self.model_dir / hdf['netGBH5']))
     self.netDA.save_weights(str(self.model_dir / hdf['netDAH5']))
     self.netDB.save_weights(str(self.model_dir / hdf['netDBH5']))
     print ("Models saved.")
コード例 #9
0
ファイル: monthly_readings.py プロジェクト: rmeineke/welldev
def main():
    logger = logbook.Logger("readings")
    database = "well.sqlite"

    db = sqlite3.connect(database)
    db.row_factory = sqlite3.Row
    cur = db.cursor()

    reading_date: str = utils.prompt_for_current_date("Reading date")
    exec_str = "INSERT INTO reading_date (date) VALUES (?)"
    params = (reading_date, )
    logger.trace(f"{exec_str}{params}")
    cur.execute(exec_str, params)
    last_inserted_row_id = cur.lastrowid

    logger.trace(f"attempting to backup the database file now")
    backup_file_name = utils.backup_file(database)
    logger.trace(f"database backed up to: {backup_file_name}")

    exec_str = "SELECT * FROM account"
    cur.execute(exec_str)
    rows = cur.fetchall()
    for r in rows:
        if r["active"] == "no":
            logger.trace(f"Account {r['acct_id']} currently INACTIVE")
            continue

        # fetch last month's reading as a sanity check
        exec_str = f"""
            SELECT reading
            FROM reading
            WHERE account_id = (?)
            ORDER BY reading_id
            DESC
        """
        params = (r["acct_id"], )
        cur.execute(exec_str, params)
        last_reading = cur.fetchone()
        print(f"Last month's reading: {last_reading['reading']}")

        # grab current reading and then insert it into the DB
        reading = input(f"{r['acct_id']} - {r['address']}: ")

        # this should allow empty input .... in case of inactive account
        if not reading:
            continue

        exec_str = f"""
            INSERT INTO reading (reading_id, account_id, reading)
            VALUES (?, ?, ?)
        """
        params = (last_inserted_row_id, r["acct_id"], reading)
        logger.trace(params)
        cur.execute(exec_str, params)

    # save, then close the cursor and db
    db.commit()
    cur.close()
    db.close()
コード例 #10
0
    def save_weights(self):
        model_dir = str(self.model_dir)
        for model in hdf.values():
            backup_file(model_dir, model)
        self.encoder.save_weights(str(self.model_dir / hdf['encoderH5']))
        self.decoder_A.save_weights(str(self.model_dir / hdf['decoder_AH5']))
        self.decoder_B.save_weights(str(self.model_dir / hdf['decoder_BH5']))

        print('saved model weights')

        serializer = Serializer.get_serializer('json')
        state_fn = ".".join([hdf['state'], serializer.ext])
        state_dir = str(self.model_dir / state_fn)
        try:
            with open(state_dir, 'wb') as fp:
                state_json = serializer.marshal({'epoch_no': self.epoch_no})
                fp.write(state_json.encode('utf-8'))
        except IOError as e:
            print(e.strerror)
コード例 #11
0
def main():
    database = "well.sqlite"

    # set up for logging
    LEVELS = {
        "debug": logging.DEBUG,
        "info": logging.INFO,
        "warning": logging.WARNING,
        "error": logging.ERROR,
        "critical": logging.CRITICAL,
    }
    if len(sys.argv) > 1:
        level_name = sys.argv[1]
        level = LEVELS.get(level_name, logging.NOTSET)
        logging.basicConfig(level=level)

    logger = logging.getLogger()
    logger.debug("Entering main")

    db = sqlite3.connect(database)
    db.row_factory = sqlite3.Row
    cur = db.cursor()

    rebate_date = utils.prompt_for_current_date("Rebate date")
    rebate_amount = utils.prompt_for_amount("Rebate amount")
    rebate_note = utils.prompt_for_notes("Notes")

    const = constants.Constants()
    exec_str = f"""
        INSERT INTO activity (date, type, amount, note) VALUES (?, ?, ?, ?)
    """
    params = (rebate_date, const._misc_rebate_received, rebate_amount,
              rebate_note)
    cur.execute(exec_str, params)
    logger.debug("attempting to backup the database file now")
    utils.backup_file(database)

    # save, then close the cursor and db
    db.commit()
    cur.close()
    db.close()
コード例 #12
0
ファイル: AutoEncoder.py プロジェクト: Valarmem/faceswap
 def save_weights(self):
     model_dir = str(self.model_dir)
     for model in hdf.values():
         backup_file(model_dir, model)
     self.encoder.save_weights(str(self.model_dir / hdf['encoderH5']))
     self.decoder_A.save_weights(str(self.model_dir / hdf['decoder_AH5']))
     self.decoder_B.save_weights(str(self.model_dir / hdf['decoder_BH5']))
     
     print('saved model weights')
     
     serializer = Serializer.get_serializer('json')
     state_fn = ".".join([hdf['state'], serializer.ext])
     state_dir = str(self.model_dir / state_fn)                        
     try:
         with open(state_dir, 'wb') as fp:
             state_json = serializer.marshal({
                 'epoch_no' : self.epoch_no
                  })
             fp.write(state_json.encode('utf-8'))
     except IOError as e:
         print(e.strerror)                   
コード例 #13
0
def main():
    database = "well.sqlite"

    # set up for logging
    LEVELS = {
        "debug": logging.DEBUG,
        "info": logging.INFO,
        "warning": logging.WARNING,
        "error": logging.ERROR,
        "critical": logging.CRITICAL,
    }
    if len(sys.argv) > 1:
        level_name = sys.argv[1]
        level = LEVELS.get(level_name, logging.NOTSET)
        logging.basicConfig(level=level)

    logger: Logger = logging.getLogger()
    logger.debug("Entering main")

    db = sqlite3.connect(database)
    db.row_factory = sqlite3.Row
    cur = db.cursor()

    logger.debug("attempting to backup the database file now")
    backup_file_name: str = utils.backup_file(logger, database)
    logger.debug(f'database backed up to: {backup_file_name}')

    reading_date = utils.prompt_for_current_date(logger, "Reading date")
    exec_str: str = "INSERT INTO reading_date (date) VALUES (?)"
    params = (reading_date, )
    logger.debug(f"{exec_str}{params}")
    cur.execute(exec_str, params)
    last_inserted_row_id = cur.lastrowid
    logger.debug(f"last_inserted_row_id: {last_inserted_row_id}")
    acct: str = utils.prompt_for_account(logger, "Please choose an account",
                                         cur)
    reading: str = input(f"Reading: ")
    exec_str = "INSERT INTO reading (reading_id, account_id, reading) VALUES (?, ?, ?)"
    params = (last_inserted_row_id, int(acct), int(reading))
    cur.execute(exec_str, params)

    # save, then close the cursor and db
    db.commit()
    cur.close()
    db.close()
コード例 #14
0
def main():
    logger = logbook.Logger('savings_dep')
    database = 'well.sqlite'

    utils.backup_file(database)
    db = sqlite3.connect(database)
    db.row_factory = sqlite3.Row
    cur = db.cursor()

    const = constants.Constants()
    exec_str = f"""
            SELECT SUM(amount)
            FROM (SELECT amount 
            FROM activity 
            WHERE type = (?)
            ORDER BY date
            DESC
            LIMIT 4)
        """
    params = (const.savings_assessment, )
    logger.trace(f"params: {params}")
    cur.execute(exec_str, params)
    last_assessment_ttl = cur.fetchone()[0]
    last_assessment_ttl = last_assessment_ttl / 100
    print(f"===============================================")
    print(f" last assessments totaled: ${last_assessment_ttl:12.2f}")

    exec_str = f"""
        SELECT SUM(amount)
        FROM activity 
        WHERE type = (?)
        OR type = (?)
        OR type = (?)
    """
    params = (const.savings_deposit_made, const.savings_disbursement,
              const.savings_dividend)
    logger.trace(f"params: {params}")
    cur.execute(exec_str, params)
    current_savings_balance = cur.fetchone()[0]
    if current_savings_balance is None:
        current_savings_balance = 0
    logger.trace(f"current_savings_balance: {current_savings_balance}")
    print(f"===============================================")
    print(f" current savings balance: ${current_savings_balance / 100:12,.2f}")
    print(f"===============================================")

    # get date_paid
    deposit_date = utils.prompt_for_current_date("Date deposit made")
    # get amount
    deposit_amount = utils.prompt_for_amount("Amount of deposit")
    # prompt for notes ... 'Dep for Jan 2019' ... or similar
    note = utils.prompt_for_notes('Notes for this deposit')

    exec_str = f"""
        INSERT INTO activity (date, type, amount, note) VALUES (?, ?, ?, ?)
    """
    params = (deposit_date, const.savings_deposit_made, deposit_amount, note)
    cur.execute(exec_str, params)

    exec_str = f"""
            SELECT SUM(amount)
            FROM activity 
            WHERE type = (?)
            OR type = (?)
            OR type = (?)
        """
    params = (const.savings_deposit_made, const.savings_disbursement,
              const.savings_dividend)
    logger.trace(f"{params}")
    cur.execute(exec_str, params)
    current_savings_balance = cur.fetchone()[0]
    logger.trace(f"current_savings_balance: {current_savings_balance}")
    print(f"===============================================")
    print(f" current savings balance: ${current_savings_balance / 100: ,.2f}")
    print(f"===============================================")

    # save, then close the cursor and db
    db.commit()
    cur.close()
    db.close()
コード例 #15
0
def main():
    database = 'well.sqlite'
    logger = logbook.Logger("savings_dividend")
    logger.debug('Entering main')
    utils.backup_file(database)
    db = sqlite3.connect(database)
    db.row_factory = sqlite3.Row
    cur = db.cursor()

    # check current balance here
    const = constants.Constants()
    exec_str = f"""
        SELECT SUM(amount)
        FROM activity 
        WHERE type = (?)
        OR type = (?)
        OR type = (?)
    """
    params = (const.savings_deposit_made, const.savings_disbursement,
              const.savings_dividend)
    logger.debug(f"params: {params}")
    cur.execute(exec_str, params)
    current_savings_balance = cur.fetchone()[0]
    logger.debug(f"current_savings_balance: {current_savings_balance}")
    print(f"===============================================")
    print(f" current savings balance: ${current_savings_balance / 100:,.2f}")
    print(f"===============================================")

    # get date_paid
    dividend_date = utils.prompt_for_current_date("Date of dividend")
    # get amount
    dividend_amount = utils.prompt_for_amount("Amount of dividend")
    # prompt for notes ... 'Dep for Jan 2019' ... or similar
    note = f"Savings dividend"

    # enter into the activity log
    exec_str = f"""
        INSERT INTO activity (date, type, amount, note) 
        VALUES (?, ?, ?, ?)
    """
    params = (dividend_date, const.savings_dividend, dividend_amount, note)
    cur.execute(exec_str, params)

    # check the balance again
    exec_str = f"""
            SELECT SUM(amount)
            FROM activity 
            WHERE type = (?)
            OR type = (?)
            OR type = (?)
        """
    params = (const.savings_deposit_made, const.savings_disbursement,
              const.savings_dividend)
    cur.execute(exec_str, params)
    current_savings_balance = cur.fetchone()[0]
    logger.debug(f"current_savings_balance: {current_savings_balance}")
    print(f"===============================================")
    print(f" current savings balance: ${current_savings_balance / 100: ,.2f}")
    print(f"===============================================")

    # save, then close the cursor and db
    db.commit()
    cur.close()
    db.close()
コード例 #16
0
def main():
    logger = logbook.Logger("bill_recd")
    database = "well.sqlite"

    # make sure this gets backed up prior to any
    # writing of the db
    utils.backup_file(database)
    db = sqlite3.connect(database)
    db.row_factory = sqlite3.Row
    cur = db.cursor()

    # prompt for amount of the bill .. and date
    bill_date = utils.prompt_for_current_date("Date of bill")
    pge_bill = float(utils.prompt_for_amount("PGE bill amount"))
    logger.trace(f"pge_bill: {int(pge_bill)}")

    const = constants.Constants()
    exec_str = f"""
        INSERT INTO activity (date, type, amount, note) 
        VALUES (?, ?, ?, ?)
    """
    params = (bill_date, const.pge_bill_received, pge_bill,
              "PGE bill received")
    cur.execute(exec_str, params)

    # instantiate an obj for each of the accounts
    cur.execute("SELECT * FROM account")
    rows = cur.fetchall()

    acct_list = []
    total_usage = 0.0

    # each row('r') ... should represent an individual account
    for r in rows:
        if r["active"] == "no":
            logger.trace(f"Account {r['acct_id']} currently INACTIVE")
            continue
        acct_obj = account.Account(
            r["acct_id"],
            r["first_name"],
            r["last_name"],
            r["file_alias"],
            r["address"],
            r["reads_in"],
            r["master"],
        )
        acct_list.append(acct_obj)

        # fetch the last two reading rows from the db
        query: str = f"""
            SELECT reading 
            FROM reading
            WHERE account_id = (?)
            ORDER BY reading_id 
            DESC LIMIT 2
        """
        params = (r["acct_id"], )
        rows = cur.execute(query, params)

        # near as I can tell this returns a row for each line of data found
        # the row is a list of selected items .... so 'reading' is the
        # zeroeth item ...
        #
        # need to collect them both in a list for further processing
        readings_list = []
        for row in rows:
            readings_list.append(
                row["reading"])  # this retrieval by name seems to be fine

        logger.trace(f"readings_list: {readings_list}")
        acct_obj.latest_reading = readings_list[0]
        acct_obj.previous_reading = readings_list[1]

        acct_obj.calculate_current_usage()
        logger.trace(f"current usage: {acct_obj.current_usage}")

        logger.trace(f"{acct_obj.reads_in} .. {acct_obj.previous_reading}")
        total_usage += acct_obj.current_usage
    total_usage = round(total_usage, 2)
    logger.trace(f"total usage: {total_usage}")

    # a balance less than $10k should trigger an assessment
    # in the upcoming for loop
    savings_balance = utils.get_savings_balance(cur)
    logger.trace(f"savings_balance: {savings_balance}")

    assessment_total = 0
    for acct in acct_list:
        logger.trace(f"\n\n{acct.addr}")

        logger.trace(
            f"current_usage_percent (b4 calculation): {acct.current_usage_percent}"
        )
        logger.trace(
            f"current_usage_percent: {(acct.current_usage / total_usage) * 100}"
        )
        logger.trace(f"total_usage: {total_usage}")

        acct.current_usage_percent = round(
            (acct.current_usage / total_usage) * 100, 2)
        logger.trace(
            f"current_usage_percent (rounded): {acct.current_usage_percent:.2f}"
        )
        logger.trace(f"pge_bill: {int(pge_bill)}")
        logger.trace(f"a.current_usage_percent: {acct.current_usage_percent}")

        acct.pge_bill_share = round(
            (pge_bill * acct.current_usage_percent / 100), 0)
        logger.trace(f"pge_bill_share: {int(acct.pge_bill_share)}")

        exec_str = f"""
            INSERT INTO activity (date, acct, type, amount, note) 
            VALUES (?, ?, ?, ?, ?)
        """
        params = (
            bill_date,
            acct.acct_id,
            const.pge_bill_share,
            acct.pge_bill_share,
            "PGE bill share",
        )
        cur.execute(exec_str, params)

        # this should be moved outside ... no sense going through all
        # this if no assessment needed ...
        # move it outside and process as separate
        if savings_balance < 1000000:
            logger.trace(f"Assessment is due.")
            acct.savings_assessment = int(
                round(acct.current_usage * const.assessment_per_gallon * 100,
                      0))
            logger.trace(f"Assessed: {acct.savings_assessment}")

            # write this to the db
            exec_str = f"""
                INSERT INTO activity (date, acct, type, amount, note) 
                VALUES (?, ?, ?, ?, ?)
            """
            params = (
                bill_date,
                acct.acct_id,
                const.savings_assessment,
                acct.savings_assessment,
                "Savings assessment",
            )
            cur.execute(exec_str, params)

            assessment_total += acct.savings_assessment
            logger.trace(
                f"Bill total: {int(round(acct.savings_assessment + acct.pge_bill_share, 2))}"
            )
        else:
            logger.trace(f"No assessment needed.")

    # added this to make the savings deposit easier
    # 2019.07.21
    #
    # 2019.08.24 ... this needed reformatting
    print(f"==============================================")
    print(f"assessment_total: ${assessment_total / 100:10.2f}")
    print(f"==============================================")
    # save, then close the cursor and db
    db.commit()
    cur.close()
    db.close()