def train(net, trainloader, optimizer_model, optimizer_centloss, criterion_softamx, criterion_centerloss, device): net.train() totoal_loss = 0 totoal_center_loss = 0 totoal_softmax_loss = 0 correct = 0 total = 0 for batch_idx, (inputs, targets) in enumerate(trainloader): inputs, targets = inputs.to(device), targets.to(device) features, logits = net(inputs) loss_softmax = criterion_softamx(logits, targets) loss_center = criterion_centerloss(features, targets) loss = loss_softmax + args.centerloss_weight * loss_center optimizer_model.zero_grad() optimizer_centloss.zero_grad() loss.backward() optimizer_model.step() # XU: I dont know why. # by doing so, weight_cent would not impact on the learning of centers for param in criterion_centerloss.parameters(): param.grad.data *= (1. / args.centerloss_weight) optimizer_centloss.step() totoal_center_loss += loss_center.item() totoal_softmax_loss += loss_softmax.item() totoal_loss += loss.item() _, predicted = logits.max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() progress_bar( batch_idx, len(trainloader), 'Loss:%.3f (Softmax: %.3f CenterLoss: %.3f) Acc: %.3f%%' % (totoal_loss / (batch_idx + 1), totoal_softmax_loss / (batch_idx + 1), totoal_center_loss / (batch_idx + 1), 100. * correct / total)) return totoal_loss / (batch_idx + 1), totoal_softmax_loss / (batch_idx + 1), \ totoal_center_loss / (batch_idx + 1), correct / total
def stage_test(net, testloader, device, name="stage1_test_normfea_doublebar"): correct = 0 total = 0 normfea_list = [] Target_list = [] with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(testloader): inputs, targets = inputs.to(device), targets.to(device) out = net(inputs) # shape [batch,class] normfea_list.append(out["norm_fea"]) Target_list.append(targets) _, predicted = (out["normweight_fea2cen"]).max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() progress_bar( batch_idx, len(testloader), '| Acc: %.3f%% (%d/%d)' % (100. * correct / total, correct, total)) print("\nTesting results is {:.2f}%".format(100. * correct / total)) normfea_list = torch.cat(normfea_list, dim=0) Target_list = torch.cat(Target_list, dim=0) energy_hist(normfea_list, Target_list, args, name)
def stage2_test(net, testloader, device): correct = 0 total = 0 pred_list, target_list, score_list = [], [], [] with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(testloader): inputs, targets = inputs.to(device), targets.to(device) out = net(inputs) threshold = out["thresholds"] # [class] sim_fea2cen = out["sim_fea2cen"] # [batch,class] dis_fea2cen = out["dis_fea2cen"] # [batch,class] b, c = dis_fea2cen.shape dis_predicted, predicted = (dis_fea2cen).min(1) #[b] compare_threshold = 1 * threshold[predicted] predicted[(dis_predicted - compare_threshold) > 0] = c pred_list.extend(predicted.tolist()) target_list.extend(targets.tolist()) score_list.extend(sim_fea2cen.tolist()) total += targets.size(0) correct += predicted.eq(targets).sum().item() progress_bar( batch_idx, len(testloader), '| Acc: %.3f%% (%d/%d)' % (100. * correct / total, correct, total)) eval_result = Evaluation(pred_list, target_list, score_list) torch.save(eval_result, os.path.join(args.checkpoint, 'eval.pkl')) print(f"accuracy is %.3f" % (eval_result.accuracy)) print(f"F1 is %.3f" % (eval_result.f1_measure)) print(f"f1_macro is %.3f" % (eval_result.f1_macro)) print(f"f1_macro_weighted is %.3f" % (eval_result.f1_macro_weighted)) print(f"area_under_roc is %.3f" % (eval_result.area_under_roc)) print(f"confuse matrix unkown is {eval_result.confusion_matrix[:,-1]}")
def test(net, testloader, device, intervals=20): energy_list = [] # energy value Target_list = [] Predict_list = [] with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(testloader): inputs, targets = inputs.to(device), targets.to(device) out = net(inputs) # shape [batch,class] energy_list.append(out["energy"]) Target_list.append(targets) _, predicted = (out['normweight_fea2cen']).max(1) Predict_list.append(predicted) progress_bar(batch_idx, len(testloader), "|||") energy_list = torch.cat(energy_list, dim=0) Target_list = torch.cat(Target_list, dim=0) Predict_list = torch.cat(Predict_list, dim=0) best_F1 = 0 best_thres = 0 best_eval = None # for these unbounded metric, we explore more intervals by *5 to achieve a relatively fair comparison. expand_factor = 5 openmetric_list = energy_list threshold_min = openmetric_list.min().item() threshold_max = openmetric_list.max().item() for thres in np.linspace(threshold_min, threshold_max, expand_factor * intervals): Predict_list[openmetric_list < thres] = args.train_class_num eval = Evaluation(Predict_list.cpu().numpy(), Target_list.cpu().numpy()) if eval.f1_measure > best_F1: best_F1 = eval.f1_measure best_thres = thres best_eval = eval print(f"Best F1 is: {best_F1} [in best threshold: {best_thres} ]") return { "best_F1": best_F1, "best_thres": best_thres, "best_eval": best_eval }
def stage2_test(net, testloader, trainloader, mixuploader, device ): correct = 0 total = 0 energy_list = [] normweight_fea2cen_list = [] Target_list = [] with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(testloader): inputs, targets = inputs.to(device), targets.to(device) out = net(inputs) # shape [batch,class] energy_list.append(out["energy"]) normweight_fea2cen_list.append(out["normweight_fea2cen"]) Target_list.append(targets) _, predicted = (out["normweight_fea2cen"]).max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() progress_bar(batch_idx, len(testloader), '| Acc: %.3f%% (%d/%d)' % (100. * correct / total, correct, total)) print("\nTesting results is {:.2f}%".format(100. * correct / total)) energy_list = torch.cat(energy_list, dim=0) Target_list = torch.cat(Target_list, dim=0) normweight_fea2cen_list = torch.cat(normweight_fea2cen_list, dim=0) energy_hist(energy_list, Target_list, args, "testing_energy") p4norm_result = normweight_fea2cen_list.norm(p=4, dim=1, keepdim=False) p3norm_result = normweight_fea2cen_list.norm(p=3, dim=1, keepdim=False) p2norm_result = normweight_fea2cen_list.norm(p=2, dim=1, keepdim=False) p1norm_result = normweight_fea2cen_list.norm(p=1, dim=1, keepdim=False) p5norm_result = normweight_fea2cen_list.norm(p=5, dim=1, keepdim=False) energy_hist(p1norm_result, Target_list, args, "stage2_p1norm_result") energy_hist(p2norm_result, Target_list, args, "stage2_p2norm_result") energy_hist(p3norm_result, Target_list, args, "stage2_p3norm_result") energy_hist(p4norm_result, Target_list, args, "stage2_p4norm_result") energy_hist(p5norm_result, Target_list, args, "stage2_p5norm_result") mixup_validate(net, trainloader, mixuploader, device, stage="2")
def stage_valmixup(net, dataloader, device, name="stage1_valtrain&sample_normfea_result"): print("validating mixup and trainloader ...") normfea_loader_list = [] normfea_mixup_list = [] target_list = [] with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(dataloader): inputs, targets = inputs.to(device), targets.to(device) mixed = mixup(inputs, targets, args) out_loader = net(inputs) out_mixed = net(mixed) normfea_loader_list.append(out_loader["norm_fea"]) normfea_mixup_list.append(out_mixed["norm_fea"]) target_list.append(targets) progress_bar(batch_idx, len(trainloader)) normfea_loader_list = torch.cat(normfea_loader_list, dim=0) normfea_mixup_list = torch.cat(normfea_mixup_list, dim=0) plot_listhist([normfea_loader_list, normfea_mixup_list], args, labels=["train data", "sampled data"], name=name) print("_______________Validate statistics:____________") print( f"train mid:{normfea_loader_list.median()} | mixup mid:{normfea_mixup_list.median()}" ) print( f"min norm:{min(normfea_loader_list.min(), normfea_mixup_list.min())} " f"| max norm:{max(normfea_loader_list.max(), normfea_mixup_list.max())}" ) return { "mid_known": normfea_loader_list.median(), "mid_unknown": normfea_mixup_list.median() }
def stage_valmixup(net, dataloader, device, name="stage1_mixup"): print("validating mixup and trainloader ...") energy_loader_list = [] energy_mixup_list = [] target_list = [] with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(dataloader): inputs, targets = inputs.to(device), targets.to(device) mixed = mixup(inputs, targets, args) out_loader = net(inputs) out_mixed = net(mixed) energy_loader_list.append(out_loader["energy"]) energy_mixup_list.append(out_mixed["energy"]) target_list.append(targets) progress_bar(batch_idx, len(trainloader)) energy_loader_list = torch.cat(energy_loader_list, dim=0) energy_mixup_list = torch.cat(energy_mixup_list, dim=0) plot_listhist([energy_loader_list, energy_mixup_list], args, labels=["loader", "mixup"], name=name + "_energy") print("_______________Validate statistics:____________") print( f"train mid:{energy_loader_list.median()} | mixup mid:{energy_mixup_list.median()}" ) print( f"min energy:{min(energy_loader_list.min(), energy_mixup_list.min())} " f"| max energy:{max(energy_loader_list.max(), energy_mixup_list.max())}" ) return { "mid_known": energy_loader_list.median(), "mid_unknown": energy_mixup_list.median(), "max_known": energy_loader_list.max(), "min_unknown": energy_mixup_list.min() }
def stage1_train(net, trainloader, optimizer, criterion, device): net.train() train_loss = 0 cls_loss = 0 dis_loss_within = 0 dis_loss_between = 0 correct = 0 total = 0 for batch_idx, (inputs, targets) in enumerate(trainloader): inputs, targets = inputs.to(device), targets.to(device) optimizer.zero_grad() out = net(inputs) loss_dict = criterion(out, targets) loss = loss_dict["total"] loss.backward() optimizer.step() train_loss += loss.item() cls_loss += loss_dict["classify"].item() dis_loss_within += loss_dict["within"].item() dis_loss_between += loss_dict["between"].item() _, predicted = (out["logits"]).max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() progress_bar( batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' % (train_loss / (batch_idx + 1), 100. * correct / total, correct, total)) return { "train_loss": train_loss / (batch_idx + 1), "cls_loss": cls_loss / (batch_idx + 1), "dis_loss_within": dis_loss_within / (batch_idx + 1), "dis_loss_between": dis_loss_between / (batch_idx + 1), "accuracy": correct / total }
def mixup_validate(net, trainloader, mixuploader, device, stage="1"): print("validating mixup ...") known_energy, unknown_energy = [], [] with torch.no_grad(): batch_idx = -1 for (inputs, targets), (inputs_bak, targets_bak) in zip(trainloader, mixuploader): batch_idx += 1 inputs, targets = inputs.to(device), targets.to(device) inputs_bak, targets_bak = inputs_bak.to(device), targets_bak.to(device) mixed = mixup(inputs, targets, inputs_bak, targets_bak, args) out_known = net(inputs) out_unkown = net(mixed) known_energy.append(out_known["energy"]) unknown_energy.append(out_unkown["energy"]) progress_bar(batch_idx, len(trainloader)) known_energy = torch.cat(known_energy, dim=0) unknown_energy = torch.cat(unknown_energy, dim=0) energy_hist_sperate(known_energy, unknown_energy, args, "mixup_stage"+stage) return{ # unkown is smaller than known "mid_known": known_energy.median().data, "mid_unknown": unknown_energy.median().data }
def stage_test(net, testloader, device, name="stage1_test_normfea_doublebar"): correct = 0 total = 0 normfea_list = [] Target_list = [] with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(testloader): inputs, targets = inputs.to(device), targets.to(device) out = net(inputs) # shape [batch,class] normfea_list.append(out["norm_fea"]) Target_list.append(targets) _, predicted = (out["normweight_fea2cen"]).max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() progress_bar( batch_idx, len(testloader), '| Acc: %.3f%% (%d/%d)' % (100. * correct / total, correct, total)) print("\nTesting results is {:.2f}%".format(100. * correct / total)) normfea_list = torch.cat(normfea_list, dim=0) Target_list = torch.cat(Target_list, dim=0) unknown_label = Target_list.max() unknown_normfea_list = normfea_list[Target_list == unknown_label] known_normfea_list = normfea_list[Target_list != unknown_label] print("_______________Testing statistics:____________") print( f"test known mid:{known_normfea_list.median()} | unknown mid:{unknown_normfea_list.median()}" ) print( f"min norm:{min(known_normfea_list.min(), unknown_normfea_list.min())} " f"| max norm:{max(known_normfea_list.max(), unknown_normfea_list.max())}" ) plot_listhist([known_normfea_list, unknown_normfea_list], args, labels=["known", "unknown"], name=name)
def receiver_loop(self): self.start_receiver() print "start Pyle Transfer Reciever on %s with port %s ..." \ % (self.recv_ip, self.recv_port) is_sender_found = False while self.status: try: if not is_sender_found: self.send_open_request() read_sockets, write_sockets, error_sockets = \ select.select(self.connections, [], [], 1) if read_sockets: send_packet, send_addr = self.recv_sock.recvfrom \ (RECV_BUFFER + HEADER_LENGTH) if "start file tranfer" in send_packet: msg, self.window_size, self.file_size = \ send_packet.split(':') self.window_size = int(self.window_size) is_sender_found = True self.file_size = int(self.file_size) self.logger.debug("window_size: %s" % self.window_size) self.logger.debug("file_size: %s" % self.file_size) self.send_addr = send_addr self.recv_sock.sendto("Come on!", self.send_addr) else: header_params = self.pkt_ext \ .get_header_params_from_packet \ (send_packet) send_seq_num = self.pkt_ext \ .get_seq_num(header_params) send_ack_num = self.pkt_ext \ .get_ack_num(header_params) send_fin_flag = self.pkt_ext \ .get_fin_flag(header_params) send_checksum = self.pkt_ext.get_checksum(header_params) log = str(datetime.datetime.now()) + " " + \ str(self.send_addr[1]) + " " + \ str(self.recv_port) + " " + \ str(send_seq_num) + " " + \ str(send_ack_num) if send_fin_flag and self.is_write_file_completed(): self.log_file.write(log + " FIN\n") send_data = self.pkt_ext \ .get_data_from_packet \ (send_packet) self.write_file_buffer(send_seq_num, send_data) self.send_close_request \ (send_seq_num, send_ack_num, send_fin_flag) self.close_receiver() print "Delivery completed successfully" else: self.log_file.write(log + "\n") if self.expected_ack == send_seq_num and \ self.pkt_ext.is_checksum_valid(send_packet): send_data = self.pkt_ext \ .get_data_from_packet \ (send_packet) self.write_file_buffer \ (send_seq_num, send_data) progress_bar(os.path.getsize(self.file_write.name), self.file_size) seq_num = send_ack_num ack_num = send_seq_num \ + RECV_BUFFER * self.window_size self.logger.debug("seq_num: %s" % seq_num) self.logger.debug("ack_num: %s" % ack_num) fin_flag = 0 packet = self.pkt_gen \ .generate_packet \ (seq_num, ack_num, fin_flag) self.recv_sock.sendto(packet, self.send_addr) self.expected_ack += RECV_BUFFER else: self.logger.debug("expected_ack not correct or packet corrupted") self.logger.debug("expected_ack: %s" % self.expected_ack) self.logger.debug("send_seq_num: %s, ignore" % send_seq_num) except KeyboardInterrupt, SystemExit: print "\nLeaving Pyle-Transfer Receiver..." self.close_receiver() os.remove(self.file_write.name)