def main(**kwargs): global args, best_prec1 mapper = { 1 : 'airplane', 2 : 'automobile', 3 : 'bird', 4 : 'cat', 5 : 'deer', 6 : 'dog', 7 : 'frog', 8 : 'horse', 9 : 'ship', 10 : 'truck' } # Override if needed for arg, v in kwargs.items(): args.__setattr__(arg, v) ### Calculate FLOPs & Param model = getattr(models, args.model)(args) if args.data in ['cifar10', 'cifar100']: IMAGE_SIZE = 32 else: IMAGE_SIZE = 224 n_flops, n_params = measure_model(model, IMAGE_SIZE, IMAGE_SIZE, args.debug) if 'measure_only' in args and args.measure_only: return n_flops, n_params print('Starting.. FLOPs: %.2fM, Params: %.2fM' % (n_flops / 1e6, n_params / 1e6)) args.filename = "%s_%s_%s.txt" % \ (args.model, int(n_params), int(n_flops)) del(model) # Create model model = getattr(models, args.model)(args) if args.debug: print(args) print(model) criterion = nn.CrossEntropyLoss() if torch.cuda.is_available(): model = torch.nn.DataParallel(model).cuda() criterion = criterion.cuda() # Define loss function (criterion) and optimizer optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay, nesterov=True) checkpoint = load_checkpoint(args) args.start_epoch = checkpoint['epoch'] + 1 best_prec1 = checkpoint['best_prec1'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) cudnn.benchmark = True ### Data loading if args.data == "cifar10": train_set = datasets.CIFAR10('../data', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ])) val_set = datasets.CIFAR10('../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), ])) elif args.data == "cifar100": train_set = datasets.CIFAR100('../data', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ])) val_set = datasets.CIFAR100('../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), ])) val_loader = torch.utils.data.DataLoader( val_set, batch_size=2, shuffle=False, num_workers=args.workers, pin_memory=True) batch_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() try: top1_per_cls = [AverageMeter() for i in range(0, model.module.num_blocks)] top5_per_cls = [AverageMeter() for i in range(0, model.module.num_blocks)] except: top1_per_cls = [AverageMeter() for i in range(0, model.num_blocks)] top5_per_cls = [AverageMeter() for i in range(0, model.num_blocks)] model.eval() end = time.time() from gradCam import * for i, (input, target) in enumerate(val_loader): if args.imgNo > 0 and i!=args.imgNo: continue (inputM, targetM) = (input, target) input, target = input[0], target[0].view(1) input = input.view(1,input.shape[0], input.shape[1],input.shape[2]) if torch.cuda.is_available(): target = target.cuda(async=True) input_var = torch.autograd.Variable(input, requires_grad=True) target_var = torch.autograd.Variable(target) # ### Compute output scores, feats = model(input_var, 0.0, p=1) if args.model == 'msdnet': loss = msd_loss(scores, target_var, criterion) else: loss = criterion(scores, target_var) ############################################################################# name = 'diags/' + mapper[target_var.cpu().data.numpy()[0]+1] + '_' + str(args.imgNo) + '_' + str(args.maxC) name2 = 'diags/' + mapper[target_var.cpu().data.numpy()[0]+1] + '_' + str(args.imgNo) if len(args.classLabel): if mapper[target_var.cpu().data.numpy()[0]+1] == args.classLabel: print(correctNess(scores, target_var), i, mapper[target_var.cpu().data.numpy()[0]+1]) elif args.imgNo < 0: print(correctNess(scores, target_var), i, mapper[target_var.cpu().data.numpy()[0]+1]) elif i == args.imgNo: import os if not os.path.exists('diags'): os.makedirs('diags') print("Category : {}".format(mapper[target_var.cpu().data.numpy()[0]+1])) print(correctNess(scores, target_var)) grad_cam = GradCam(model = model) mask = grad_cam(target_var.cpu().data.numpy()[0], input_var, 0, args.maxC-1)#target_index) gb_model = GuidedBackpropReLUModel(model) gb = gb_model(target_var.cpu().data.numpy()[0], input_var, 0, args.maxC-1).transpose(2,0,1) img = input[0].cpu().data.numpy().transpose(1,2,0) img = cv2.resize(img, (512, 512)) show_cam_on_image(img, mask, name) utils.save_image(torch.from_numpy(gb), name +'_gb.jpg') cam_mask = np.zeros(gb.shape) for i in range(0, gb.shape[0]): cam_mask[i, :, :] = mask cam_gb = np.multiply(cam_mask, gb) utils.save_image(torch.from_numpy(cam_gb), name +'_cam_gb.jpg') img = cv2.resize(input.cpu().data.numpy()[0].transpose(1,2,0), (512, 512)) utils.save_image(torch.from_numpy(img.transpose(2,0,1)), name2 + '_input.jpg') exit() else: continue
def main(**kwargs): global args, best_prec1 # Override if needed for arg, v in kwargs.items(): args.__setattr__(arg, v) ### Calculate FLOPs & Param model = getattr(models, args.model)(args) if args.data in ['cifar10', 'cifar100']: IMAGE_SIZE = 32 else: IMAGE_SIZE = 224 n_flops, n_params = measure_model(model, IMAGE_SIZE, IMAGE_SIZE, args.debug) if 'measure_only' in args and args.measure_only: return n_flops, n_params print('Starting.. FLOPs: %.2fM, Params: %.2fM' % (n_flops / 1e6, n_params / 1e6)) args.filename = "%s_%s_%s.txt" % \ (args.model, int(n_params), int(n_flops)) del (model) # Create model model = getattr(models, args.model)(args) if args.debug: print(args) print(model) model = torch.nn.DataParallel(model).cuda() # Define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay, nesterov=True) # Resume from a checkpoint if args.resume: checkpoint = load_checkpoint(args) if checkpoint is not None: args.start_epoch = checkpoint['epoch'] + 1 best_prec1 = checkpoint['best_prec1'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) # Evaluate from a model if args.evaluate_from is not None: args.evaluate = True state_dict = torch.load(args.evaluate_from)['state_dict'] model.load_state_dict(state_dict) cudnn.benchmark = True ### Data loading if args.data == "cifar10": train_set = datasets.CIFAR10('../data', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ])) val_set = datasets.CIFAR10('../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), ])) elif args.data == "cifar100": train_set = datasets.CIFAR100('../data', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ])) val_set = datasets.CIFAR100('../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), ])) train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) val_loader = torch.utils.data.DataLoader(val_set, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) # Run Forward / Backward passes if args.evaluate: validate(val_loader, model, criterion) return for epoch in range(args.start_epoch, args.epochs): # Train for one epoch tr_prec1, tr_prec5, loss, lr = \ train(train_loader, model, criterion, optimizer, epoch) # Evaluate on validation set val_prec1, val_prec5 = validate(val_loader, model, criterion) # Remember best prec@1 and save checkpoint is_best = val_prec1 < best_prec1 best_prec1 = max(val_prec1, best_prec1) model_filename = 'checkpoint_%03d.pth.tar' % epoch save_checkpoint( { 'epoch': epoch, 'model': args.model, 'state_dict': model.state_dict(), 'best_prec1': best_prec1, 'optimizer': optimizer.state_dict(), }, args, is_best, model_filename, "%.4f %.4f %.4f %.4f %.4f %.4f\n" % (val_prec1, val_prec5, tr_prec1, tr_prec5, loss, lr)) # TestModel and return model = model.cpu().module model = nn.DataParallel(model).cuda() print(model) validate(val_loader, model, criterion) n_flops, n_params = measure_model(model, IMAGE_SIZE, IMAGE_SIZE) print('Finished training! FLOPs: %.2fM, Params: %.2fM' % (n_flops / 1e6, n_params / 1e6)) print('Please run again with --resume --evaluate flags,' ' to evaluate the best model.') return
def main(**kwargs): global args for arg, v in kwargs.items(): args.__setattr__(arg, v) print(args) program_start_time = time.time() instanceName = "classification_Accuracy" folder_path = os.path.dirname(os.path.abspath(__file__)) timestamp = datetime.datetime.now() ts_str = timestamp.strftime('%Y-%m-%d-%H-%M-%S') path = folder_path + os.sep + instanceName + os.sep + args.model + os.sep + ts_str + "_" + args.dataset + "_" + args.wordembedding if args.debug: print("[Debug mode]") path = folder_path + os.sep + instanceName + os.sep + "Debug-" + args.model + os.sep + ts_str + "_" + args.dataset + "_" + args.wordembedding else: path = folder_path + os.sep + instanceName + os.sep + args.model + os.sep + ts_str + "_" + args.dataset + "_" + args.wordembedding os.makedirs(path) args.savedir = path global logFile logFile = path + os.sep + "log.txt" if args.model == "BiLSTMConv": Model = models.BiLSTMConv # elif args.model == "BiGRU": # Model = models.BiGRU # elif args.model == "WordCNN": # Model = models.WordCNN # elif args.model == "BiGRUWithTimeDropout": # Model = models.BiGRUWithTimeDropout elif args.model == "CNN_Text_Model": Model = CNN_Text_Model.CNN_Text elif args.model == "VDCNN": Model = vcdnn.VDCNN else: NotImplementedError # process the input data. captionStrDict = {"fig_title": args.dataset, "x_label": "epoch"} train_iter, test_iter, net = data_preprocess.prepare_data_and_model( Model=Model, args=args, using_gpu=True) print("args: ", args) LOG(str(args), logFile) global device device = 'cuda' if torch.cuda.is_available() else 'cpu' device = 'cuda' net = net.to(device) if device == 'cuda': net = torch.nn.DataParallel(net).cuda() cudnn.benchmark = True optimizer = optim.Adam(params=net.parameters(), lr=1e-3, weight_decay=1e-4) lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=1000, gamma=.99) if device == "cuda": criterion = nn.CrossEntropyLoss().cuda() else: criterion = nn.CrossEntropyLoss() # criterion = nn.CrossEntropyLoss().cuda() best_test_acc = 0 best_test_results = [] ground_truth = [] epoch_train_accs = [] epoch_train_losses = [] epoch_test_accs = [] epoch_test_losses = [] epoch_lrs = [] for epoch in range(args.epochs): epoch_start_time = time.time() train_accs = [] train_losses = [] for batch in tqdm(train_iter): lr_scheduler.step() net.train() xs = batch.text ys = batch.label # # ys = ys.squeeze(1) # print("ys_train data type: ", type(ys)) # print("ys_train: ", ys) if device == 'cuda': ys = ys.cuda(async=True) # ys = torch.autograd.Variable(ys) xs = torch.autograd.Variable(xs) ys_var = torch.autograd.Variable(ys) # print(ys_var) logits = net(xs) loss = criterion(logits, ys_var) # print("loss: ", loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() train_losses.append(loss.item() / int(args.batch_size)) train_accs.append(accuracy(logits.data, ys)) train_accs_normal = [i[0].item() for i in train_accs] # print("epoch ", epoch, " : training accumulated accuracy ", np.mean(train_accs_normal)) LOG("epoch: " + str(epoch), logFile) LOG("[TRAIN] accumulated accuracy: " + str(np.mean(train_accs_normal)), logFile) epoch_train_accs.append(np.mean(train_accs_normal)) epoch_train_losses.append(np.mean(train_losses)) test_accs = [] test_losses = [] test_predict_results = [] best_test_acc = 0 net.eval() pred_results = [] print("running testing.....") for batch in tqdm(test_iter): xs_test = batch.text ys_test = batch.label logits_test = net(xs_test) test_loss = criterion(logits_test, ys_test) test_losses.append(test_loss.item() / int(args.batch_size)) test_accs.append(accuracy(logits_test.data, ys_test)) pred_results = pred_results + logits_test.topk( 1, 1, True, True)[1].t().cpu().numpy().tolist()[0] if epoch == (args.epochs - 1): ground_truth = ground_truth + ys_test.cpu().numpy().tolist() test_accs_normal = [i[0].item() for i in test_accs] # print("epoch {} : testing accumulated accuracy {} %".format(epoch, np.mean(test_accs))) print("epoch ", epoch, " : testing accumulated accuracy ", np.mean(test_accs_normal)) # LOG("epoch: "+ str(epoch), logFile) LOG("[TEST] accumulated accuracy: " + str(np.mean(test_accs_normal)), logFile) if best_test_acc < np.mean(test_accs_normal): best_test_acc = np.mean(test_accs_normal) best_test_results = pred_results torch.save(net.state_dict(), path + os.sep + str(Model.name) + ".pkl") epoch_test_accs.append(np.mean(test_accs_normal)) epoch_test_losses.append(np.mean(test_losses)) # epoch_lrs.append(0.1) try: lr = float(str(optimizer[-1]).split("\n")[-5].split(" ")[-1]) except: lr = 100 epoch_lrs.append(lr) log_stats(path, [np.mean(train_accs_normal)], [np.mean(train_losses)], [np.mean(test_accs_normal)], [np.mean(test_losses)], lr) one_epoch_last_time = time.time() - epoch_start_time LOG("last time: " + str(one_epoch_last_time), logFile) df = pd.DataFrame(data={ "test_label": best_test_results, "ground truth": ground_truth }) df.to_csv(path + os.sep + "test_classification_result.csv", sep=',', index=True) # save the metrics report logFile = confusion_matrix(df["test_label"], df["ground truth"], logFile) # # # here plot figures # # algos\Classification_Accuracy\CNN_Text_Model\2019-01-23-14-58-01_tripadvisor\test_acc.txt # import pandas as pd # # algos\Classification_Accuracy\BiLSTMConv\\2019-01-22-10-29-54_tripadvisor\test_acc.txt # epoch_test_accs = list(pd.read_csv("algos\\Classification_Accuracy\\CNN_Text_Model\\2019-01-23-14-58-01_tripadvisor\\test_acc.txt", header=None).iloc[:,0]) # epoch_train_accs = list(pd.read_csv("algos\\Classification_Accuracy\\CNN_Text_Model\\2019-01-23-14-58-01_tripadvisor\\train_acc.txt", header=None).iloc[:,0]) # epoch_train_losses = list(pd.read_csv("algos\\Classification_Accuracy\\CNN_Text_Model\\2019-01-23-14-58-01_tripadvisor\\train_losses.txt", header=None).iloc[:,0]) # epoch_test_losses = list(pd.read_csv("algos\\Classification_Accuracy\\CNN_Text_Model\\2019-01-23-14-58-01_tripadvisor\\test_losses.txt", header=None).iloc[:,0]) plot_figs(epoch_train_accs, epoch_train_losses, epoch_test_accs, epoch_test_losses, args, captionStrDict) LOG("============Finish============", logFile)
def main(**kwargs): global args lowest_error1 = 100 for arg, v in kwargs.items(): args.__setattr__(arg, v) program_start_time = time.time() instanceName = "Classification_Accuracy" folder_path = os.path.dirname( os.path.abspath(__file__)) + os.sep + args.model timestamp = datetime.datetime.now() ts_str = timestamp.strftime('%Y-%m-%d-%H-%M-%S') path = folder_path + os.sep + instanceName + os.sep + args.model_name + os.sep + ts_str tensorboard_folder = path + os.sep + "Graph" os.makedirs(path) args.savedir = path writer = SummaryWriter(tensorboard_folder) global logFile logFile = path + os.sep + "log.txt" args.filename = logFile global num_outputs print(args) global device device = 'cuda' if torch.cuda.is_available() else 'cpu' if args.data == "cifar100" or args.data == "CIFAR100": fig_title_str = " on CIFAR-100" elif args.data == "cifar10" or args.data == "CIFAR10": fig_title_str = " on CIFAR-10" elif args.data == "tiny_imagenet": fig_title_str = " on tiny_imagenet" else: LOG( "ERROR =============================dataset should be CIFAR10 or CIFAR100", logFile) NotImplementedError captionStrDict = { "fig_title": fig_title_str, "x_label": "epoch", 'elastic_final_layer_label': "Final_Layer_Output_Classifier", "elastic_intermediate_layer_label": "Intermediate_Layer_Classifier_" } # save input parameters into log file LOG("program start time: " + ts_str + "\n", logFile) # if args.layers_weight_change == 1: # LOG("weights for intermediate layers: 1/(34-Depth), giving different weights for different intermediate layers output, using the formula weigh = 1/(34-Depth)", logFile) # elif args.layers_weight_change == 0: # LOG("weights for intermediate layers: 1, giving same weights for different intermediate layers output as 1", logFile) # else: # print("Parameter --layers_weight_change, Error") # sys.exit() if args.model == "Elastic_ResNet18" or args.model == "Elastic_ResNet34" or args.model == "Elastic_ResNet50" or args.model == "Elastic_ResNet101" or args.model == "Elastic_ResNet152": model = Elastic_ResNet(args, logFile) elif args.model == "Elastic_InceptionV3": args.target_size = ( 299, 299, 3 ) # since pytorch inceptionv3 pretrained accepts image size (299, 299, 3) instead of (224, 224, 3) model = Elastic_InceptionV3(args, logFile) elif args.model == "Elastic_MobileNet": model = Elastic_MobileNet(args, logFile) elif args.model == "Elastic_VGG16": model = Elastic_VGG16_bn(args, logFile) elif args.model == "Elastic_SqueezeNet": model = Elastic_SqueezeNet(args, logFile) elif args.model == "Elastic_DenseNet121" or args.model == "Elastic_DenseNet169" or args.model == "Elastic_DenseNet201": model = Elastic_DenseNet(args, logFile) else: LOG( "--model parameter should be in ResNet, InceptionV3, MobileNet, VGG16, SqueezeNet, DenseNet", logFile) exit() num_outputs = model.num_outputs # num_outputs = 1 LOG("num_outputs: " + str(num_outputs), logFile) LOG("successfully create model: " + args.model, logFile) args_str = str(args) LOG(args_str, logFile) model = model.to(device) if device == 'cuda': model = torch.nn.DataParallel(model).cuda() cudnn.benchmark = True # TUT thinkstation data folder path data_folder = "/media/yi/e7036176-287c-4b18-9609-9811b8e33769/tiny_imagenet/tiny-imagenet-200" # narvi data folder path # data_folder = "/home/zhouy/data/tiny-imagenet-200" # XPS 15 laptop data folder path # data_folder = "D:\Elastic\data" # args.batch_size = 1 summary(model, (3, 224, 224)) if args.data == "tiny_imagenet": train_loader, test_loader = tiny_image_data_loader(data_folder, args) else: train_loader = get_train_loader(args.data, data_dir=data_folder, batch_size=args.batch_size, augment=False, target_size=args.target_size, random_seed=20180614, valid_size=0.2, shuffle=True, show_sample=False, num_workers=4, pin_memory=True, debug=args.debug) test_loader = get_test_loader(args.data, data_dir=data_folder, batch_size=args.batch_size, shuffle=True, target_size=args.target_size, num_workers=4, pin_memory=True, debug=args.debug) criterion = nn.CrossEntropyLoss().cuda() if args.data != "tiny_imagenet": pretrain_optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad, model.parameters()), args.pretrain_learning_rate, momentum=args.momentum, weight_decay=args.weight_decay) LOG("==> Pretraining for **1** epoches \n", logFile) for pretrain_epoch in range(0, 1): accs, losses, lr = train(train_loader, model, criterion, pretrain_optimizer, pretrain_epoch) epoch_result = " pretrain epoch: " + str( pretrain_epoch) + ", pretrain error: " + str( accs) + ", pretrain loss: " + str( losses) + ", pretrain learning rate: " + str( lr) + ", pretrain total train sum loss: " + str( sum(losses)) LOG(epoch_result, logFile) summary(model, (3, 224, 224)) LOG("==> Full training \n", logFile) for param in model.parameters(): param.requires_grad = True optimizers = [] childs = [] k = 0 for child in model.parameters(): childs.append(child) k += 1 # childs_params = [childs[:9], childs[:15], childs[:21], childs[:27], # childs[:33], childs[:39], childs[:45], childs[:51], # childs[:57], childs[:63], childs[:69], childs[:75], childs] childs_params = [childs[:25], childs[:43], childs[:61], childs] for i in range(num_outputs): optimizer = torch.optim.SGD(childs_params[i], args.learning_rate, momentum=args.momentum, weight_decay=args.weight_decay) optimizers.append(optimizer) # optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate, betas=(0.9, 0.999), eps=1e-08, weight_decay=args.weight_decay) # summary(model, (3,224,224)) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', threshold=1e-4, patience=10) # implement early stop by own EarlyStopping_epoch_count = 0 epochs_train_accs = [] epochs_train_top5_accs = [] epochs_train_losses = [] epochs_test_accs = [] epochs_test_losses = [] epochs_lr = [] epochs_test_top5_accs = [] for epoch in range(0, args.epochs): epoch_str = "==================================== epoch %d ==============================" % epoch LOG(epoch_str, logFile) # Train for one epoch accs, losses, lr, accs_top5 = train(train_loader, model, criterion, optimizers, epoch) epochs_train_accs.append(accs) epochs_train_losses.append(losses) epochs_lr.append(lr) epochs_train_top5_accs.append(accs_top5) writer.add_scalar(tensorboard_folder + os.sep + "data" + os.sep + 'lr', lr, epoch) for i, a, l, k in zip(range(len(accs)), accs, losses, accs_top5): writer.add_scalar( tensorboard_folder + os.sep + "data" + os.sep + 'train_error_' + str(i), a, epoch) writer.add_scalar( tensorboard_folder + os.sep + "data" + os.sep + 'train_losses_' + str(i), l, epoch) writer.add_scalar( tensorboard_folder + os.sep + "data" + os.sep + 'train_top5_error_' + str(i), k, epoch) epoch_result = "\ntrain error: " + str(accs) + "top 5 error: " + str( accs_top5) + ", \nloss: " + str( losses) + ", \nlearning rate " + str( lr) + ", \ntotal train sum loss " + str(sum(losses)) LOG(epoch_result, logFile) if num_outputs > 1: writer.add_scalar( tensorboard_folder + os.sep + "data" + os.sep + 'train_total_sum_losses', sum(losses), epoch) losses.append(sum(losses)) # add the total sum loss LOG("train_total_sum_losses: " + str(sum(losses)), logFile) # run on test dataset LOG("==> test \n", logFile) test_accs, test_losses, test_top5_accs = validate( test_loader, model, criterion) epochs_test_accs.append(test_accs) epochs_test_losses.append(test_losses) epochs_test_top5_accs.append(test_top5_accs) for i, a, l, k in zip(range(len(test_accs)), test_accs, test_losses, test_top5_accs): writer.add_scalar( tensorboard_folder + os.sep + "data" + os.sep + 'test_error_' + str(i), a, epoch) writer.add_scalar( tensorboard_folder + os.sep + "data" + os.sep + 'test_losses_' + str(i), l, epoch) writer.add_scalar( tensorboard_folder + os.sep + "data" + os.sep + 'test_top5_losses_' + str(i), k, epoch) test_result_str = "==> Test epoch: \nfinal output classifier error: " + str( test_accs ) + "test top 5 error: " + str(test_top5_accs) + ", \ntest_loss" + str( test_losses) + ", \ntotal test sum loss " + str(sum(test_losses)) LOG(test_result_str, logFile) total_loss = sum(test_losses) if num_outputs > 1: writer.add_scalar( tensorboard_folder + os.sep + "data" + os.sep + 'test_total_sum_losses', total_loss, epoch) test_losses.append(total_loss) # add the total sum loss LOG("test_total_sum_losses: " + str(total_loss), logFile) log_stats(path, accs, losses, lr, test_accs, test_losses, accs_top5, test_top5_accs) # Remember best prec@1 and save checkpoint is_best = test_accs[ -1] < lowest_error1 #error not accuracy, but i don't want to change variable names if is_best: lowest_error1 = test_accs[-1] #但是有个问题,有时是倒数第二个CLF取得更好的结果 save_checkpoint( { 'epoch': epoch, 'model': args.model_name, 'state_dict': model.state_dict(), 'best_prec1': lowest_error1, 'optimizer': optimizer.state_dict(), }, args) # apply early_stop with monitoring val_loss # EarlyStopping(patience=15, score_function=score_function(val_loss), trainer=model) scheduler.step(total_loss) # adjust learning rate with test_loss if epoch == 0: prev_epoch_loss = total_loss # use all intemediate classifiers sum loss instead of only one classifier loss else: if total_loss >= prev_epoch_loss: # means this current epoch doesn't reduce test losses EarlyStopping_epoch_count += 1 if EarlyStopping_epoch_count > 20: LOG( "No improving test_loss for more than 10 epochs, stop running model", logFile) break # n_flops, n_params = measure_model(model, IMAGE_SIZE, IMAGE_SIZE) # FLOPS_result = 'Finished training! FLOPs: %.2fM, Params: %.2fM' % (n_flops / 1e6, n_params / 1e6) # LOG(FLOPS_result, logFile) # print(FLOPS_result) writer.close() end_timestamp = datetime.datetime.now() end_ts_str = end_timestamp.strftime('%Y-%m-%d-%H-%M-%S') LOG("program end time: " + end_ts_str + "\n", logFile) # here plot figures plot_figs(epochs_train_accs, epochs_train_losses, epochs_test_accs, epochs_test_losses, args, captionStrDict) LOG("============Finish============", logFile)
def main(**kwargs): global args # ============================================================================= # lowest_error1 = 100 # ============================================================================= for arg, v in kwargs.items(): args.__setattr__(arg, v) args.batch_size = 1 device = 'cuda' if torch.cuda.is_available() else 'cpu' # TUT thinkstation data folder path data_folder = "/media/yi/e7036176-287c-4b18-9609-9811b8e33769/Elastic/data" # narvi data folder path # data_folder = "/home/zhouy/Elastic/data" # XPS 15 laptop data folder path #data_folder = "D:\Elastic\data" # ============================================================================= # train_loader, val_loader = get_train_valid_loader(args.data, data_dir=data_folder, batch_size=args.batch_size, augment=False, # random_seed=20180614, valid_size=0.2, shuffle=True,show_sample=False, # num_workers=1,pin_memory=True) # ============================================================================= test_loader = get_test_loader(args.data, data_dir=data_folder, batch_size=args.batch_size, shuffle=True, target_size = (224,224,3), num_workers=4,pin_memory=True) # ============================================================================= # test_loader = get_test_loader(args.data, data_dir=data_folder, batch_size=args.batch_size, shuffle=True, target_size = args.target_size, # num_workers=4,pin_memory=True) # ============================================================================= #model = vgg16_bn(pretrained=True) model = VGG(make_layers(cfg['D'], batch_norm=True)) fc_features = model.classifier[6].in_features model.classifier[6] = nn.Linear(fc_features, 100) model = model.to(device) model.cuda() if device == 'cuda': model = torch.nn.DataParallel(model).cuda() cudnn.benchmark = True criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), args.learning_rate, momentum=args.momentum, weight_decay=args.weight_decay, nesterov=False)# nesterov set False to keep align with keras default settting model.train() for i, (input, target) in enumerate(test_loader): # lr = adjust_learning_rate(optimizer, epoch, args, batch=i, nBatch=len(train_loader)) # data_time.update(time.time() - end) summary(model, (3,224,224)) target = target.cuda(async=True) input_var = torch.autograd.Variable(input) target_var = torch.autograd.Variable(target) #print("target size: ", target_var.shape()) # ============================================================================= # losses = 0 # ============================================================================= optimizer.zero_grad() outputs = model(input_var) loss = criterion(outputs, target_var) # 这里应该要再封装一下, 变成只有一个变量loss loss.backward() optimizer.step() summary(model, (3,224,224))
def main(**kwargs): global args for arg, v in kwargs.items(): args.__setattr__(arg, v) # program_start_time = time.time() instanceName = "classification_Accuracy" folder_path = os.path.dirname(os.path.abspath(__file__)) timestamp = datetime.datetime.now() ts_str = timestamp.strftime('%Y-%m-%d-%H-%M-%S') path = folder_path + os.sep + instanceName + os.sep + args.model + os.sep + ts_str + "_" + args.dataset os.makedirs(path) args.savedir = path global logFile logFile = path + os.sep + "log.txt" LOG(str(args), logFile) X_train, y_train, X_test, y_test = prepare_data_svm(args) LOG( "train data size : " + str(len(y_train)) + " test data size : " + str(len(y_test)), logFile) if args.model == "liblinear_svm": # # liblinear_svm prediction # y_pred_SVM, time_SVM = liblinear_svm(X_train, y_train, X_test, y_test) # LOG("time elapse: " + str(time_SVM), logFile) # LOG("multiclass liblinear SVM: " + str(accuracy_score(y_test, y_pred_SVM)), logFile) # SVM_predictions_csv = np.column_stack((X_test, y_pred_SVM)) # SVM_predictions_csv.to_csv(path + os.sep + "test_classification_result.csv", sep=',', index=True) pass elif args.model == "svm" or args.model == "SVM": # SVM prediction y_pred, time = clf_SVM(X_train, y_train, X_test) accuracy = accuracy_score(y_test, y_pred) LOG("time elapse: " + str(time) + " seconds", logFile) LOG("[SVM] accuracy: " + str(accuracy), logFile) df = pd.DataFrame(data={ "test review": X_test, "test_label": y_pred, "ground truth": y_test }) df.to_csv(path + os.sep + "test_classification_result.csv", sep=',', index=True) else: NotImplementedError LOG("============Finish============", logFile) # svm_out_path ="liblinear_SVM_prediction_4rd_run.csv" # with open(svm_out_path, 'w') as f: # csv.writer(f).writerows(SVM_predictions_csv) # f.close() # # first test on VADER system # y_pred_VADER, time_VADER = clf_VADER(X_test) # print("VADER elapsed time: ", round(time_VADER, 2), " s") # # # os.makedirs(prefix_path) # # # Save the evaluation to a csv # VADER_predictions_csv= np.column_stack((X_test, y_pred_VADER)) # # vader_out_path = "VADER_prediction.csv" # with open(vader_out_path, 'w') as f: # csv.writer(f).writerows(VADER_predictions_csv) # f.close() # # find these reviews which are wrongly classified # X_test = list(X_test) # y_test = list(y_test) # # wrong_clf_reviews_VADER = dict() # # wrong_clf_reviews_list = list() # print("test size length: ", len(y_test)) # # assert len(y_test) == len(y_pred_VADER) # # for i in range(len(y_pred_VADER)): # if y_pred_VADER[i] != y_test[i]: # wrong_clf_reviews_list.append([y_pred_VADER[i], y_test[i], i, X_test[i], "VADER"]) # else: # pass # #calculate confusion matrix logFile = confusion_matrix(y_pred, y_test, logFile) # save misclassified reviews wrong_clf_reviews = save_misclassified_reviews(X_test, y_pred, y_test, args.model) wrong_clf_reviews.to_csv(path + os.sep + "wrong_clf_reviews.csv", sep=',', index=True)
def main(**kwargs): global args, best_prec1, acc_file_name, flops_file_name # Override if needed # kwargs is a dict for arg, v in kwargs.items(): args.__setattr__(arg, v) # set args.arg=v, copy kwargs to args ### Calculate FLOPs & Param model = getattr(models, args.model)(args) ###### ? get models.(args.model) if args.data in ['cifar10', 'cifar100']: IMAGE_SIZE = 32 if (args.data == 'cifar10'): acc_file_name = "cifar10_acc.csv" flops_file_name = "cifar10_flops.csv" else: acc_file_name = "cifar100_acc.csv" flops_file_name = "cifar100_flops.csv" else: IMAGE_SIZE = 224 set_save_path() # calculate the FLOPs # if (args.evaluate): n_flops, n_params = measure_model(model, IMAGE_SIZE, IMAGE_SIZE, flops_file_path, args.debug) ##### if 'measure_only' in args and args.measure_only: # no 'measure_only' parameter return n_flops, n_params print('Starting.. FLOPs: %.2fM, Params: %.2fM' % (n_flops / 1e6, n_params / 1e6)) args.filename = "%s_%s_%s.txt" % (args.model, int(n_params), int(n_flops)) # del(model) # Create model(the 2nd) # model = getattr(models, args.model)(args) #### if args.debug: print(args) # print(model) model = torch.nn.DataParallel( model).cuda() # Implements data parallelism at the module level # if exist mulit-devices, This container parallelizes the application of the given module by splitting # the input across the specified devices by chunking in the batch dimension (other objects will be copied once per device) ### Data loading, no nomarlisation if args.data == "cifar10": train_set = datasets.CIFAR10('../data', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ])) val_set = datasets.CIFAR10('../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), ])) elif args.data == "cifar100": train_set = datasets.CIFAR100('../data', train=True, download=True, transform=transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), ])) val_set = datasets.CIFAR100('../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), ])) cudnn.benchmark = True ###### train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) val_loader = torch.utils.data.DataLoader(val_set, batch_size=args.batch_size, shuffle=False, num_workers=args.workers, pin_memory=True) # test_loader = torch.utils.data.DataLoader( # val_set, # batch_size=args.batch_size, shuffle=False, # num_workers=args.workers, pin_memory=True) test_loader = val_loader # Define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), args.lr, momentum=args.momentum, weight_decay=args.weight_decay, nesterov=True) ##### validate # Resume from a checkpoint if args.resume: # use latest checkpoint if have any checkpoint = load_checkpoint(args) if checkpoint is not None: args.start_epoch = checkpoint['epoch'] + 1 best_prec1 = checkpoint['best_prec1'] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) # Evaluate from a model if args.evaluate_from is not None: # path to saved checkpoint args.evaluate = True state_dict = torch.load(args.evaluate_from)['state_dict'] model.load_state_dict(state_dict) if args.evalmode is not None: if args.evalmode == 'anytime': validate(val_loader, model, criterion) else: print('dynamic') dynamic_evaluate(model, test_loader, val_loader, args) return # Run Forward / Backward passes # evaluate and return # if args.evaluate: # validate(val_loader, model, criterion) # return ###### train best_epoch = 0 for epoch in range(args.start_epoch, args.epochs): # Train for one epoch tr_prec1, tr_prec5, loss, lr = train(train_loader, model, criterion, optimizer, epoch) # Evaluate on validation set val_prec1, val_prec5 = validate(val_loader, model, criterion) # Remember best prec@1 and save checkpoint is_best = val_prec1 < best_prec1 best_prec1 = max(val_prec1, best_prec1) if (is_best): best_epoch = epoch model_filename = 'checkpoint_%03d.pth.tar' % epoch save_checkpoint( { 'epoch': epoch, 'model': args.model, 'state_dict': model.state_dict(), 'best_prec1': best_prec1, 'optimizer': optimizer.state_dict(), }, args, is_best, model_filename, "%.4f %.4f %.4f %.4f %.4f %.4f\n" % (val_prec1, val_prec5, tr_prec1, tr_prec5, loss, lr)) print('Best val_prec1: {:.4f} at epoch {}'.format(best_prec1, best_epoch)) # TestModel and return model = model.cpu().module model = nn.DataParallel(model).cuda() # if args.debug: # print(model) validate(val_loader, model, criterion) # n_flops, n_params = measure_model(model, IMAGE_SIZE, IMAGE_SIZE, flops_file_path,args.debug) # print('Finished training! FLOPs: %.2fM, Params: %.2fM' % (n_flops / 1e6, n_params / 1e6)) print('Please run again with --resume --evaluate flags,' ' to evaluate the best model.') return