def register(): # 读取文件,查看文件里是否有数据。如果文件不存在,默认是一个字典 data = file_manager.read_json('data.json', {}) while True: teacher_name = input('请输入账号(3~6位):') if not 3 <= len(teacher_name) <= 6: print('账号不符合要求,请重新输入!') else: break if teacher_name in data: print('注册失败!该账号已经注册过!') return while True: password = input('请输入密码(6~12位):') if not 6 <= len(password) <= 12: print('密码不符合要求,请重新输入!') else: break # 用户名密码都已经输入正确以后,创建一个teacher对象 t = model.Teacher(teacher_name, password) data[t.name] = t.password file_manager.write_json('data.json', data)
def create_model(flags, input_ids, seg_ids, input_mask, labels, n_label, is_training): bs_per_core = tf.shape(input_ids)[0] in_ids = tf.transpose(input_ids, [1, 0]) seg_ids = tf.transpose(seg_ids, [1, 0]) input_mask = tf.transpose(input_mask, [1, 0]) labels = tf.reshape(labels, [bs_per_core]) xlnet_model = model.Teacher( flags=flags, input_ids=in_ids, seg_ids=seg_ids, input_mask=input_mask ) output = xlnet_model.get_output() with tf.variable_scope("model/loss", reuse=tf.AUTO_REUSE): if is_training: output = tf.nn.dropout(output, rate=flags.dropout) logit = tf.keras.layers.Dense(n_label, kernel_initializer=xln._get_initializer(flags))(output) log_prob = tf.nn.log_softmax(logit, axis=-1) one_hot_labels = tf.one_hot(labels, depth=n_label, dtype=tf.float32) per_sample_loss = -tf.reduce_sum(one_hot_labels*log_prob, axis=-1) loss = tf.reduce_mean(per_sample_loss) return loss, log_prob, logit
def register(): # 读取文件,查看文件中有没有数据 data = file_manager.read_json('data.json', {}) while True: teacher_name = input('请输入您的用户名:(3~12位):') if not 3 <= len(teacher_name) <= 12: print('用户名不符合要求,请重新输入') else: break if teacher_name in data: print('注册失败!该账号已经注册过!') return while True: password = input('请输入您的密码:(6~12):') if not 6 <= len(password) <= 12: print('您输入密码有误,请重新输入') else: break # 用户密码都已经输入正确以后,创建一个teacher对象 t = model.Teacher(teacher_name, password) data[t.name] = t.password # data[teacher_name] = password # print(teacher) file_manager.write_json('data.json', data)
def register(): data = file_manager.read_json('data.json', {}) while True: teacher_name = input('请输入账号(3-6位):') if not 3 <= len(teacher_name) <= 6: print('账号不符合要求,请重新输入!') else: break if teacher_name in data: print('注册失败!该账户已经被注册了!') return while True: password = input('请输入账号(6-12位):') if not 6 <= len(password) <= 12: print('账号不符合要求,请重新输入!') else: break # 创建一个teacher对象,保存数据。 t = model.Teacher(teacher_name, password) data[t.name] = t.password file_manager.write_json('data.json', data)
def register(): # 读取json文件,如果文件不存在默认是个字典 data = file_manager.read_json('data') while True: teacher_name = input('请输入用户名(3~6位):') if not 3 <= len(teacher_name) <= 6: print('不符合要求,请重新输入!') else: break while True: password = input('请输入密码(6~12位):') if not 6 <= len(password) <= 12: print('不符合要求,请重新输入!') else: break # 输入正确后可创建一个对象 teacher = model.Teacher(data, password) # 建议将密码加密保存 data[teacher_name] = teacher.password file_manager.write_json('data', data)
def register(): while True: user_name = input('请输入账号(3~6位):') if 3 <= len(user_name) <= 6: break print('账号不符合要求,请重新输入!') all_user = file_manager.read_json(USERS_FILE, {}) if user_name in all_user: print("注册失败!该账号已经注册过!") return while True: password = input('请输入密码(6~12位):') if 6 <= len(password) <= 12: break print('密码不符合要求,请重新输入!') s = model.Teacher(user_name, password) all_user[s.name] = s.password file_manager.write_json(USERS_FILE, all_user)
def register(): # 读取文件,查看文件里是否有数据。如果文件不存在,默认是一个字典 data = file_manager.read_json('data.json', {}) while True: teacher_name = input("请输入账号(3-6位)") if not 3 <= len(teacher_name) <= 6: print("账号输入有误") else: break if teacher_name in data: print("该账号已经注册") return while True: teacher_password = input("请输入密码(6-12位)") if not 6 <= len(teacher_password) <= 12: print("密码输入有误") else: break t = model.Teacher(teacher_name, tools.encrypt_password(teacher_password)) data[t.name] = t.password file_manager.write_json("data.json", data)
def register(): # 读取文件,查看文件里是否有数据。如果文件不存在,默认是一个字典。 data = file_manager.read_json('data.json', {}) while True: teacher_name = input('请输入账号(3—6位):') if not 3 <= len(teacher_name) <= 6: print('账号不符合要求,请重新输入。') else: break if teacher_name in data: print('注册失败,该账号已经注册过了。') return while True: password = input('请输入密码(6—12位):') if not 6 <= len(password) <= 12: print('密码不符合要求,请重新输入。') else: break # 用户名和密码都创建正确以后,创建一个teacher对象。 t = model.Teacher(teacher_name, password) data[t.name] = t.password file_manager.write_json('data.json', data)
def main(args): data_fn = 'plots/prec_cov_list.pk' fig_fn = 'plots/prec_cov' if os.path.exists(data_fn): pc_data = pickle.load(open(data_fn, 'rb')) plot_prec_cov(pc_data['T_list'], pc_data['prec_list'], pc_data['cov_list'], fig_fn) return ## init a snapshot path os.makedirs(args.train.save_root, exist_ok=True) ## init logger sys.stdout = Logger(os.path.join(args.train.save_root, 'out')) ## print args print_args(args) ## init gpus if not args.cpu: print("##GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) print() ## init datasets print("## init datasets") ds_src = data.MultiSourceDataset( args.data.src, args.aug_params, batch_size=args.data.batch_size, val_shuffle=True, val_aug=True, domain_id=1, color=False if args.data.img_size[2] == 1 else True, size=args.data.img_size[0], sample_ratio=args.data.sample_ratio[0]) assert (len(args.aug_params) == 1) ##TODO ds_tar = getattr(data, args.data.tar)( root=os.path.join('data', args.data.tar.lower()), batch_size=args.data.batch_size, aug_list=args.aug_params[0], val_shuffle=True, val_aug=True, domain_id=0, color=False if args.data.img_size[2] == 1 else True, size=args.data.img_size[0], sample_ratio=args.data.sample_ratio[1]) ds_dom = data.DomainDataset( data.MultiSourceDataset( args.data.src, args.aug_params, batch_size=args.data.batch_size, val_shuffle=True, val_aug=True, test_aug=True, #diff domain_id=1, color=False if args.data.img_size[2] == 1 else True, size=args.data.img_size[0], sample_ratio=args.data.sample_ratio[0]), getattr(data, args.data.tar)( root=os.path.join('data', args.data.tar.lower()), batch_size=args.data.batch_size, aug_list=args.aug_params[0], val_shuffle=True, val_aug=True, test_aug=True, #diff domain_id=0, color=False if args.data.img_size[2] == 1 else True, size=args.data.img_size[0], sample_ratio=args.data.sample_ratio[1])) print() #### ## reliable teacher learning #### mdl_st_base = getattr(model, args.model.base)(num_class=args.model.n_labels, input_shape=args.model.img_size) #mdl_st_base = model.TempCls(mdl_st_base) mdl_st = model.Student(args.model, mdl_st_base, ds_src, ds_tar, ideal=args.ideal) mdl_tc_base = getattr(model, args.model.base)(num_class=args.model.n_labels, input_shape=args.model.img_size) #mdl_tc_base = model.TempCls(mdl_tc_base) mdl_tc = model.Teacher(args.model, mdl_tc_base, ds_src, ds_tar, ideal=args.ideal) ## rename model_t = mdl_tc model_s = mdl_st model_c = model_s.model_base params = args.train params_base = args.train_base params_advtr = args.train_advtr params_iw = args.train_iw params_iw_cal = args.cal_iw params_conf = args.est_conf i_epoch = 1 ## init a model if params.init == 'sourceonly': ##TODO: assume classification print("## init the student model with sourceonly training") model.set_trainable(model_c, True) ## init a learner learner = LearnerCls(params_base, model_c, model_name_postfix='_sourceonlyinit') ## train the model learner.train(ds_src.train, ds_src.val, ds_src.test) ## test the model learner.test(ds_src.test, ld_name='src', verbose=True) print() elif params.init == 'advtr': ##TODO: assume classification print("## init a base model with adversarial training") model.set_trainable(model_c, True) ## init a adv model mdl_adv = getattr(model, params_advtr.model_advtr)(n_in=model_c.dim_feat) ## init a learner learner = LearnerDACls(params_advtr, model.DAN(model_c, mdl_adv), model_name_postfix='_advtrinit') ## train the model learner.train([ds_src.train, ds_dom.train], None, ds_tar.test) ## test the model learner.test(ds_tar.test, ld_name='tar', verbose=True) print() else: raise NotImplementedError ## init iw if model_t.train.model_conf.model_iw is not None: print("## learn IW") model_sd = model_t.train.model_conf.model_iw.model_sd.model model_sd.train() ## init a learner learner_sd = LearnerCls(params_iw, model_sd, model_name_postfix='_iw_epoch_%d' % (i_epoch)) ## train the model learner_sd.train(ds_dom.train, ds_dom.val, ds_dom.test) ## test the model learner_sd.test(ds_dom.test, ld_name='domain', verbose=True) print() ## init a calibraton model model_sd_cal = model_t.train.model_conf.model_iw.model_sd model_sd_cal.train() ## init a calibrator calibrator_iw = CalibratorCls(params_iw_cal, model_sd_cal, model_name_postfix='_iw_cal_epoch_%d' % (i_epoch)) ## calibrate the model calibrator_iw.train(ds_dom.val, ds_dom.val, ds_dom.test) ## test the model calibrator_iw.test(ds_dom.test, ld_name='domain', verbose=True) print() ## 2. learn confidence predictor model_base = model_t.train.model_base model_conf = model_t.train.model_conf model_iw = model_t.train.model_conf.model_iw model_iw_cond = model.CondIW(model_iw, model_conf, ds_src.train, ds_tar.train) ## init a learner learner = LearnerConfPred(params_conf, model_conf, model_base, model_iw_cond, model_name_postfix='_confpred_epoch_%d' % (i_epoch)) ## train the model learner.train(ds_src.val, ds_src.val, ds_tar.test) ## test the model learner.test(ds_tar.test, ld_name='tar', verbose=True) learner.test(ds_tar.train, ld_name='tar (train)', verbose=True) print() else: model_base = model_t.train.model_base model_conf = model_t.train.model_conf ## init a learner learner = LearnerConfPred(params_conf, model_conf, model_base, None, model_name_postfix='_confpred_epoch_%d' % (i_epoch)) ## train the model model_conf.T = tf.Variable(1.0 - params_conf.eps) ##TODO print("T = %f" % (model_conf.T.numpy())) ## test the model learner.test(ds_tar.test, ld_name='tar', verbose=True) learner.test(ds_tar.train, ld_name='tar (train)', verbose=True) ## compute precision and coverage T_list, prec_list, cov_list = [], [], [] for T in np.arange(0.0, 1.0, 0.01): model_conf.T = T prec, n_conf, n = learner.test(ds_tar.train, ld_name='tar (train)', verbose=True) T_list.append(T) prec_list.append(prec.numpy()) cov_list.append(float(n_conf) / float(n)) print(T_list) print(prec_list) print(cov_list) print() T_list = np.array(T_list) prec_list = np.array(prec_list) cov_list = np.array(cov_list) pickle.dump( { 'T_list': T_list, 'prec_list': prec_list, 'cov_list': cov_list }, open(data_fn, 'wb'))
school2 = model.School('8002', '上海前程似锦', '浦东xxxx') data = {} data[school1.schoolID] = school1 data[school2.schoolID] = school2 common.pickleDump(data, school_db) # 课程记录 course1 = model.Course('1', 'Linux', 6500.00, '8001') course2 = model.Course('2', 'Python', 5500.00, '8001') course3 = model.Course('3', 'Go', 4500.00, '8002') data = {} data[course1.courseID] = course1 data[course2.courseID] = course2 data[course3.courseID] = course3 common.pickleDump(data, course_db) # 老师 teacher1 = model.Teacher('1', 'oldboy', 'oldboy', common.md5Encode('123456'), '8001') teacher2 = model.Teacher('2', 'alex', 'alex', common.md5Encode('123456'), '8001') teacher3 = model.Teacher('3', 'jack', 'jack', common.md5Encode('123456'), '8002') data = {} data[teacher1.id] = teacher1 data[teacher2.id] = teacher2 data[teacher3.id] = teacher3 common.pickleDump(data, teacher_db) # 班级信息 classes1 = model.Classes('1', '1', '1', '20周', '2017-09-01', '8001') classes2 = model.Classes('2', '2', '2', '20周', '2017-09-27', '8001') classes3 = model.Classes('3', '3', '3', '18周', '2017-10-18', '8002') data = {} data[classes1.classesID] = classes1
def get_results_self(args, ds_src, ds_tar, ideal=False): from learning import LearnerClsSelf from learning import TempScalingCls as CalibratorCls #exp_name_list = glob.glob('snapshots/m2m_self_small_?') exp_name_list = [ 'snapshots/m2m_self_small%s_0' % ('_ideal' if ideal else ''), 'snapshots/m2m_self_small%s_1' % ('_ideal' if ideal else ''), 'snapshots/m2m_self_small%s_2' % ('_ideal' if ideal else ''), 'snapshots/m2m_self_small%s_3' % ('_ideal' if ideal else ''), #'snapshots/m2m_self_small_4', 'snapshots/m2m_self_small%s_5' % ('_ideal' if ideal else ''), 'snapshots/m2m_self_small%s_6' % ('_ideal' if ideal else ''), ] error_cls_list, error_cal_list = [], [] for exp_name in exp_name_list: params = types.SimpleNamespace() params.save_root = exp_name params.find_best = False params.load_final = False print(params.save_root) ## init a base model mdl_st_base = getattr(model, args.model.base)(num_class=args.data.n_labels, input_shape=args.data.img_size) mdl_st_base = model.TempCls(mdl_st_base) mdl_st = model.Student(args.model, mdl_st_base, ds_src, ds_tar, ideal=ideal) #mdl_tc_base = getattr(model, args.model.base)(num_class=args.model.n_labels, input_shape=args.model.img_size) #mdl_tc_base = model.TempCls(mdl_tc_base) mdl_tc_base = mdl_st_base #shared model mdl_tc = model.Teacher(args.model, mdl_tc_base, ds_src, ds_tar, ideal=ideal) ## init a learner learner = LearnerClsSelf(params, None, None, mdl_st, mdl_tc, ideal=ideal) ## train the model learner.train(ds_src, None, None) ##TODO: teacher model contains the loaders ## test the model error_cls, error_cal, *_ = learner.test(ds_tar.test, ld_name=args.data.tar, verbose=True) error_cls_list.append(error_cls.numpy()) error_cal_list.append(error_cal) return np.array(error_cls_list), np.array(error_cal_list)