def main(config, resume): # setup data_loader instances data_loader = getattr(module_loaders, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], batch_size=512, shuffle=False, validation_split=0.0, training=False, num_workers=2) # build model architecture module_model = getattr(module_models, config['model']['type']) model = get_instance(module_model, 'arch', config['model']) model.summary() # get function handles of loss and metrics loss_fn = getattr(module_model, config['model']['loss']) metric_fns = [ getattr(module_model, met) for met in config['model']['metrics'] ] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): data, target = data.to(device), target.to(device) output = model(data) # # save sample images, or do something with output here # # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = data.shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output, target) * batch_size n_samples = len(data_loader.sampler) log = {'loss': total_loss / n_samples} log.update({ met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) }) print(log)
def main(config, resume): print("Opening video capture...") print("Building model...") # build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict print("Loading Model...") checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: print("Running on {} GPUS...".format(config['n_gpu'])) model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) print("Model Loaded...") print() print() q = Q(18) fps = FPS().start() transform = module_data.eval_transform for i in range(18): _ = q.read() with open('/u/big/trainingdata/20BNJESTER/jester-v1-labels.csv', 'r') as f: pred_to_class = {} csv_reader = csv.reader(f) for i, row in enumerate(csv_reader): pred_to_class[i] = row[0] with torch.no_grad(): while True: frames = q.read() q.show_frame(frames) frames = transform(frames) data = frames.to(device) output = model(data) ind = torch.argmax(output).item() print(pred_to_class[ind] + ' ' * 50, end='\r', flush=True)
def main(config, resume): # setup data_loader instances data_loader = get_instance(module_data, 'data_loader', config, tokenizer, ) # build model architecture model = get_instance(module_arch, 'arch', config, data_loader.train_vocab.vectors) model.summary() # get function handles of loss and metrics # loss = getattr(module_loss, config['loss']['type']) metrics = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() scores = [] with torch.no_grad(): for batch_idx, data in enumerate(data_loader.test_iter): input = build_data(data, device) output =model(input) # loss = self.loss(output[0], output[1], self.config['loss']['t0'], self.device) # self.writer.set_step((epoch - 1) * len(self.valid_data_loader) + batch_idx, 'valid') # self.writer.add_scalar('loss', loss.item()) # record the score to eval scores.extend(output[0].tolist()) # evaluate total_val_metrics = eval_metrics(scores, data_loader, metrics) print('MAP:', total_val_metrics)
def main(config, resume): # setup data_loader instances data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], config['data_loader']['args']['csv_path'], img_size=config['data_loader']['args']['img_size'], batch_size=1, shuffle=False, validation_split=0.0, training=False, num_workers=0) # build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() sample_submission = pd.read_csv(config['input_csv']) os.makedirs("./submit", exist_ok=True) thresholds = [0.4, 0.5] for threshold in thresholds: filenames, labels, submissions = [], [], [] with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): data = data.to(device) output = model(data) label = output.sigmoid().cpu().data.numpy() filenames.append(target) labels.append(label > threshold) for row in np.concatenate(labels): subrow = ' '.join(list([str(i) for i in np.nonzero(row)[0]])) submissions.append(subrow) sample_submission['Predicted'] = submissions sample_submission.to_csv( "./submit/submission-{0:.2f}.csv".format(threshold), index=None)
def main(config, resume): # setup data_loader instances data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], batch_size=512, shuffle=False, validation_split=0.0, training=False, num_workers=2) # build model architecture model = get_instance(module_arch, 'arch', config) print(model) # get function handles of loss and metrics #loss_fn = getattr(module_loss, config['loss']) #metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() #total_loss = 0.0 #total_metrics = torch.zeros(len(metric_fns)) name = config['name'] + '2.csv' #'dnn.csv' with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): data, target = data.to(device), target.to(device) if i == 0: predictions = model(data).cpu().numpy() else: predictions = np.vstack( (predictions, model(data).cpu().numpy())) # computing loss, metrics on test set #loss = loss_fn(output, target) #batch_size = data.shape[0] #total_loss += loss.item() * batch_size #for i, metric in enumerate(metric_fns): # total_metrics[i] += metric(output, target) * batch_size write_result(name, predictions)
def __init__(self, config, neuron_seq, resume): # a list of neurons with sequence self.neuron_seq = neuron_seq # setup data_loader instances self.data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], batch_size=512, shuffle=False, validation_split=0.0, training=False, num_workers=2) # build model architecture self.model = get_instance(module_arch, 'arch', config) # model.summary() # get function handles of loss and metrics self.loss_fn = getattr(module_loss, config['loss']) self.metric_fns = [ getattr(module_metric, met) for met in config['metrics'] ] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: self.model = torch.nn.DataParallel(self.model) self.model.load_state_dict(state_dict) # prepare model for testing self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') self.model = self.model.to(self.device) # register hook # self.model.register_forward_hook(print) # self.model.eval() # for index, layer in enumerate(self.model): # print(index, layer) self.ablation(1, 5)
def main(config, resume): '''data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], batch_size=512, shuffle=False, validation_split=0.0, training=False, num_workers=2, )''' model = get_instance(module_arch, 'arch', config) #model.summary() #loss_fn = getattr(module_loss, config['loss']) #metric_fns = [getattr(module_metric, met) for met in config['metrics']] checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] model.load_state_dict(state_dict) model.eval() example = torch.rand(1, 2) traced_script_module = torch.jit.trace(model, example) output = traced_script_module(torch.tensor([[-0.8037, -0.2691]])) print(output) traced_script_module.save("model.pt")
def main(config, resume, infile, outfile, sigma, dur, half): # build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) if config['n_gpu'] > 1: model = model.module model.apply(remove_weight_norms) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() sr = config['arch']['args']['sr'] y, _ = load(infile, sr=sr, duration=dur) y = torch.Tensor(y).to(device) # get mel before turn to half, because sparse.half is not implement yet mel = model.get_mel(y[None, :]) if half: model = model.half() mel = mel.half() start = time() x = model.infer(mel, sigma) cost = time() - start print("Time cost: {:.4f}, Speed: {:.4f} kHz".format( cost, x.numel() / cost / 1000)) # print(x.max(), x.min()) write_wav(outfile, x.cpu().float().numpy(), sr, False)
def main(config, resume): # build model architecture model = get_instance(module_arch, 'generator_arch', config) model.summary() # load state dict checkpoint = torch.load(resume, map_location='cpu') #if 'GAN' in config["name"] or 'PG' in config["name"] or 'Real' in config["name"]: state_dict = checkpoint['generator_state_dict'] #else: # state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() evaluator = UnetEvaluator(model, config) evaluator.evaluate()
else: print("Using CPU for computation") return args #------------------------------------------------------------------------------ # Main execution #------------------------------------------------------------------------------ if __name__ == '__main__': """ python grad_cam.py <path_to_image> 1. Loads an image with opencv. 2. Preprocesses it for VGG19 and converts to a pytorch variable. 3. Makes a forward pass to find the category index with the highest score, and computes intermediate activations. Makes the visualization. """ args = get_args() config = json.load(open(args.config)) model = get_instance(module_arch, 'arch', config) model.load_pretrained_model(args.weight) grad_cam = GradCam(model=model, use_cuda=args.use_cuda) img = cv2.imread(args.image)[..., ::-1] img = np.float32( cv2.resize(img, (256, 256), interpolation=cv2.INTER_LINEAR)) / 255 input = preprocess_image(img) mask = grad_cam(input, index=None) show_cam_on_image(img, mask, outfile="pics/grad_cam.jpg")
def test_alphacom(config, resume): config['data_loader']['args']['batch_size'] = 1 test_data_loader = alpha_com_dataloader(config) batch_size = config['data_loader']['args']['batch_size'] # build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # get function handles of loss and metrics # loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() # 一般有drop 或者 bn用这个 with torch.no_grad(): # for i, data in enumerate(tqdm(test_data_loader)): for i, data in enumerate(test_data_loader): image = data[0][0].permute(1, 2, 0) h, w, c = image.size() new_h = h - h % 32 new_w = w - w % 32 # print(image.size(), h, w) image = image.numpy() # print(image.shape, new_h, new_w) image = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_LINEAR) # print(image.shape) image = torch.from_numpy(image).permute( 2, 0, 1)[np.newaxis, :, :, :].to(device) trimap1 = data[1][0].permute(1, 2, 0) trimap2 = data[2][0].permute(1, 2, 0) trimap3 = data[3][0].permute(1, 2, 0) image_name = data[4][0] # print(trimap1.size()) trimap1 = trimap1.numpy() # print(trimap1.shape) # for i in trimap1: print(i) # cv2.imshow("213414",trimap1) # cv2.waitKey(0) trimap1 = cv2.resize(trimap1, (new_w, new_h), interpolation=cv2.INTER_LINEAR) trimap1 = torch.from_numpy(trimap1)[np.newaxis, np.newaxis, :, :].to(device) trimap2 = trimap2.numpy() trimap2 = cv2.resize(trimap2, (new_w, new_h), interpolation=cv2.INTER_LINEAR) trimap2 = torch.from_numpy(trimap2)[np.newaxis, np.newaxis, :, :].to(device) trimap3 = trimap3.numpy() trimap3 = cv2.resize(trimap3, (new_w, new_h), interpolation=cv2.INTER_LINEAR) trimap3 = torch.from_numpy(trimap3)[np.newaxis, np.newaxis, :, :].to(device) # print(image.size(), trimap1.size(), trimap2.size(), trimap3.size()) if config['arch']['args']['stage'] == 0: pred1 = model(torch.cat((image, trimap1), dim=1)) pred2 = model(torch.cat((image, trimap2), dim=1)) pred3 = model(torch.cat((image, trimap3), dim=1)) else: _, pred1 = model(torch.cat((image, trimap1), dim=1)) _, pred2 = model(torch.cat((image, trimap2), dim=1)) _, pred3 = model(torch.cat((image, trimap3), dim=1)) pred1 *= 255 pred2 *= 255 pred3 *= 255 pred1 = pred1[0].permute(1, 2, 0).cpu().numpy() image1 = get_final_output(pred1, trimap1) image1 = cv2.resize(image1, (w, h), interpolation=cv2.INTER_LINEAR) pred2 = pred2[0].permute(1, 2, 0).cpu().numpy() image2 = get_final_output(pred1, trimap2) image2 = cv2.resize(image2, (w, h), interpolation=cv2.INTER_LINEAR) pred3 = pred3[0].permute(1, 2, 0).cpu().numpy() image3 = get_final_output(pred1, trimap3) image3 = cv2.resize(image3, (w, h), interpolation=cv2.INTER_LINEAR) # print(image.size()) # print(pred1.shape,pred2.shape,pred3.shape) # pred3 = cv2.resize(pred3, (w, h), interpolation=cv2.INTER_LINEAR) cv2.imwrite("alpha_com_output/Trimap1/{}".format(image_name), image1) cv2.imwrite("alpha_com_output/Trimap2/{}".format(image_name), image2) cv2.imwrite("alpha_com_output/Trimap3/{}".format(image_name), image3) print(i)
def DIM_test(config, resume): # setup data_loader instances config['data_loader']['args']['batch_size'] = 1 config['data_loader']['args']['usage'] = 'test' config['data_loader']['args']['validation_split'] = 0 test_data_loader = get_instance(module_data, 'data_loader', config, config) batch_size = config['data_loader']['args']['batch_size'] # build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # get function handles of loss and metrics # loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing model.eval() # 一般有drop 或者 bn用这个 total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) cot = 0 total_SAD = 0.0 total_MSE = 0.0 total_loss = 0.0 with torch.no_grad(): # for i, data in enumerate(tqdm(test_data_loader)): for i, data in enumerate(test_data_loader): image = data[0].to(device) alpha = data[1].to(device) fg = data[2].to(device) bg = data[3].to(device) trimap = data[4].to(device) if config['arch']['args']['stage'] == 0: raw_alpha_pred = model(torch.cat((image, trimap), dim=1)) else: raw_alpha_pred, refine_alpha_pred = model( torch.cat((image, trimap), dim=1)) # # save sample images, or do something with output here # # # computing loss, metrics on test set # loss = loss_fn(output, target) w = 0.5 # 0.25 0.75 loss = w * overall_loss(image, alpha, raw_alpha_pred, trimap, fg, bg) + \ (1 - w) * alpha_prediction_loss(alpha, refine_alpha_pred, trimap) metric_s = '' acc_metrics = np.zeros(len(metric_fns)) # 清空list for j, metric in enumerate(metric_fns): # 对所有的metrics进行评测 acc_metrics[j] += metric(refine_alpha_pred, alpha, trimap) metric_s += metric.__name__ + ': ' + str( acc_metrics[j]) + ' ' total_MSE += acc_metrics[0] total_SAD += acc_metrics[1] refine_alpha_pred *= 255 for j in range(batch_size): cv2.imwrite("test_output/{}original.jpg".format(cot), image[j].permute(1, 2, 0).cpu().numpy()) cv2.imwrite( "test_output/{}alpha_matte.jpg".format(cot), refine_alpha_pred[j].permute(1, 2, 0).cpu().numpy()) cot += 1 total_loss += loss.item() * batch_size # loss是取平均的 print("test{}/{}: loss: {} ".format( i, len(test_data_loader), loss / (batch_size), )) # for i, metric in enumerate(metric_fns): # total_metrics[i] += metric(output, target) * batch_size print("avg_LOSS: {} avg_SAD: {}, avg_MSE: {}".format( total_loss / len(test_data_loader), total_SAD / len(test_data_loader), total_MSE / len(test_data_loader)))
def main(config, resume): # setup data_loader instances data_loader = get_instance(module_data, 'data_loader', config) #data_loader = getattr(module_data, config['data_loader']['type'])( # config['data_loader']['args'] # batch_size=512, # shuffle=False, # validation_split=0.0, # training=False, # num_workers=2 #) data_loader = data_loader.split_validation() # build model architecture model = import_module('model', config)(**config['model']['args']) model.summary() # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) predictions = {"output": [], "target": []} with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): #data, target = data.to(device), target.to(device) target = target.to(device) output = model(data, device) # # save sample images, or do something with output here # output, logits = output predictions['output'].append(output.cpu().numpy()) predictions['target'].append(target.cpu().numpy()) # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = target.shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output, target) * batch_size n_samples = len(data_loader.sampler) log = {'loss': total_loss / n_samples} log.update({ met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) }) print(log) save_dir = os.path.join(os.path.abspath(os.path.join(resume, '..', '..'))) predictions['output'] = np.hstack(predictions['output']) predictions['target'] = np.hstack(predictions['target']) print(save_dir + '/predictions.pkl') with open(os.path.join(save_dir, 'predictions.pkl'), 'wb') as handle: pickle.dump(predictions, handle, protocol=pickle.HIGHEST_PROTOCOL)
def main(config, resume): # setup data_loader instances data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], batch_size=512, shuffle=False, validation_split=0.0, training=False, num_workers=2, ) # build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) rho_pred = [] rho_orig = [] T_pred = [] T_orig = [] mu_pred = [] mu_orig = [] cp_pred = [] cp_orig = [] psi_pred = [] psi_orig = [] alpha_pred = [] alpha_orig = [] as_pred = [] as_orig = [] with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): data, target = data.to(device), target.to(device) output = model(data) rho_pred.append(output[:, 0]) rho_orig.append(target[:, 0]) T_pred.append(output[:, 1]) T_orig.append(target[:, 1]) mu_pred.append(output[:, 2]) mu_orig.append(target[:, 2]) cp_pred.append(output[:, 3]) cp_orig.append(target[:, 3]) psi_pred.append(output[:, 4]) psi_orig.append(target[:, 4]) alpha_pred.append(output[:, 5]) alpha_orig.append(target[:, 5]) as_pred.append(output[:, 6]) as_orig.append(target[:, 6]) # # save sample images, or do something with output here # # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = data.shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output, target) * batch_size rho_pred = [item for sublist in rho_pred for item in sublist] rho_orig = [item for sublist in rho_orig for item in sublist] T_pred = [item for sublist in T_pred for item in sublist] T_orig = [item for sublist in T_orig for item in sublist] mu_pred = [item for sublist in mu_pred for item in sublist] mu_orig = [item for sublist in mu_orig for item in sublist] cp_pred = [item for sublist in cp_pred for item in sublist] cp_orig = [item for sublist in cp_orig for item in sublist] psi_pred = [item for sublist in psi_pred for item in sublist] psi_orig = [item for sublist in psi_orig for item in sublist] alpha_pred = [item for sublist in alpha_pred for item in sublist] alpha_orig = [item for sublist in alpha_orig for item in sublist] as_pred = [item for sublist in as_pred for item in sublist] as_orig = [item for sublist in as_orig for item in sublist] fig, arr_sp = plt.subplots(2, 4) arr_sp[0, 0].scatter(array(rho_orig), array(rho_pred), label='rho', c='r') arr_sp[0, 1].scatter(array(T_orig), array(T_pred), label='T', color='orange') arr_sp[0, 2].scatter(array(mu_orig), array(mu_pred), label='thermo:mu', color='yellow') arr_sp[0, 3].scatter(array(cp_orig), array(cp_pred), label='Cp', color='green') arr_sp[1, 0].scatter(array(psi_orig), array(psi_pred), label='thermo:psi', color='blue') arr_sp[1, 1].scatter(array(alpha_orig), array(alpha_pred), label='thermo:alpha', color='indigo') arr_sp[1, 2].scatter(array(as_orig), array(as_pred), label='thermo:as', color='violet') fig.delaxes(arr_sp[1, 3]) fig.legend(loc='lower right', prop={'size': 22}) n_samples = len(data_loader.sampler) log = {'loss': total_loss / n_samples} log.update({ met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) }) print(log)
def main(config, resume): # setup data_loader instances data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['file'], config['data_loader']['args']['batch_size'], config['data_loader']['args']['max_seq_len'], config['data_loader']['args']['num_bands'], shuffle=False, validation_split=0.0, num_workers=2) # build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) # directory for saving images save_dir = os.path.split(resume)[0] save_dir = os.path.join(save_dir, 'test') ensure_dir(save_dir) with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): data, target = data.to(device), target.to(device) states = model.init_hidden(data.size(0)) output = model(data, states) # prepare figures for display gt = target.cpu().numpy() # (batch, time, :) mu = output['pred_mean'][0].detach().cpu().numpy() for b in range(gt.shape[0]): gt_ = gt[b].reshape(gt.shape[1], 32, 3) mu_ = mu.reshape(mu.shape[0], 32, 3) for j in range(32): img_prefix = os.path.join( save_dir, 'idx_' + str(i * gt.shape[0] + b) + '_jt_' + str(j)) gt__ = gt_[:, j, :] mu__ = mu_[:, j, :] fig = make_figure(gt__) fig.savefig(img_prefix + '_gt.png') plt.close(fig) fig = make_figure(mu__) fig.savefig(img_prefix + '_pred.png') plt.close(fig) # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = data.shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output, target) * batch_size n_samples = len(data_loader.sampler) log = {'loss': total_loss / n_samples} log.update({ met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) }) print(log)
def interpolate(source, output, naming_scheme, config, args): # build model architecture model = get_instance(module_arch, 'arch', config) #model.summary() # load state dict checkpoint = torch.load(args.resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() pre_transform = T.Compose([ UniformSample(config["data_loader"]["args"]["num_points"]), NormalizeScale() ]) with torch.no_grad(): f1 = None f2 = None for i, open3d_pc in enumerate(tqdm(source)): np_pc = open3d_to_np_pc(open3d_pc) pc = torch.FloatTensor(np_pc) # If this is first iteration if f2 is None: f2 = pc continue # Shift the frames f1 = f2 f2 = pc pos_cat = torch.cat([f1, f2]) data = Data(pos=pos_cat) graph_id = torch.zeros(data.pos.size()[0]) graph_id[f1.size()[0]:] = 1 data.graph_id = graph_id data = pre_transform(data) pc1 = data.pos[data.graph_id == 0].to(device) pc2 = data.pos[data.graph_id == 1].to(device) batch1 = torch.zeros(pc1.size()[0], dtype=torch.int64).to(device) batch2 = torch.zeros(pc2.size()[0], dtype=torch.int64).to(device) out = model(pc1, pc2, batch1, batch2) idx = 2 * (i - 1) # print("f1 min ", torch.min(f1, dim=0)) # print("f1 max ", torch.max(f1, dim=0)) # print("pc1 min ", torch.min(pc1, dim=0)) # print("pc1 max ", torch.max(pc1, dim=0)) output.add_np_pc(naming_scheme.format(idx + 0), pc1.cpu().data.numpy()) output.add_np_pc(naming_scheme.format(idx + 1), out.cpu().data.numpy()) output.add_np_pc(naming_scheme.format(idx + 2), pc2.cpu().data.numpy())
def main(test_config): # load model architecture model_path = test_config["trained_model_path"] model_config = torch.load(model_path)["config"] model = import_module("model", model_config)(**model_config["model"]["args"]) model.summary() # setup data_loader instances data_loader = get_instance(module_data, "data_loader", test_config) weight = data_loader.dataset.get_pos_weight() print(weight) # get function handles of loss and metrics loss_fn = getattr(module_loss, model_config["loss"]) metric_fns = [getattr(module_metric, met) for met in model_config["metrics"]] # load state dict checkpoint = torch.load(model_path) state_dict = checkpoint["state_dict"] if model_config["n_gpu"] > 1: model = torch.nn.DaaParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) model.eval() total_loss = 0.0 total_metrics = np.zeros(len(metric_fns)) predictions = {"output": [], "target": []} with torch.no_grad(): for data, target in data_loader: target = target.to(device) if len(target.shape) == 0: target = target.unsqueeze(dim=0) output = model(data, device) if model_config["loss"] == "bce_loss": output, _ = model(data, device=device) elif model_config["loss"] == "bce_loss_with_logits": _ ,output= model(data, device=device) predictions["output"].append(output.cpu().numpy()) predictions["target"].append(target.cpu().numpy()) # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = target.shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output, target) * batch_size del output del target n_samples = len(data_loader.sampler) log = {"loss": total_loss / n_samples} log.update( { met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) } ) predictions["output"] = np.hstack(predictions["output"]) predictions["target"] = np.hstack(predictions["target"]) print(len(data_loader.dataset), n_samples, predictions["output"].shape, predictions["target"].shape) total_metrics[-2] = pr_auc_1(predictions["output"], predictions["target"]) total_metrics[-1] = roc_auc_1(predictions["output"], predictions["target"]) log.update({metric_fns[-2].__name__: total_metrics[-2]}) log.update({metric_fns[-1].__name__: total_metrics[-1]}) print(log) save_dir = os.path.join(os.path.abspath(os.path.join(model_path, ".."))) with open(os.path.join(save_dir, "predictions.pkl"), "wb") as handle: pickle.dump(predictions, handle, protocol=pickle.HIGHEST_PROTOCOL) with open(os.path.join(save_dir, "test-results.pkl"), "wb") as handle: pickle.dump(log, handle, protocol=pickle.HIGHEST_PROTOCOL)
def main(config, resume): # set visualization preference outputOverlaycsv = False showHeatMap = False # setup data_loader instances data_loader = get_instance(module_data, 'data_loader_test', config) ''' data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], batch_size=512, shuffle=False, validation_split=0.0, training=False, num_workers=2 ) ''' # build model architecture model = get_instance(module_arch, 'arch', config) print(model) if torch.cuda.is_available(): print("Using GPU: " + torch.cuda.get_device_name(0)) else: print("Using CPU to test") # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) #classes = ('endothelium', 'pct', 'vasculature') classes = ('endothelium', 'pct') all_pred = [] all_true = [] all_softmax = [] if showHeatMap: hm_layers = {'final_layer': 'layer', 'fc_layer': 'fc_layer', 'conv_num': 17, 'fc_num': 3} #need to set based on model heatmapper = classActivationMap.CAMgenerator(hm_layers, config, model) #heatmapper = classActivationMap.CAMgenerator3d(hm_layers, config, model) #for 3d data heatmapper.generateImage(num_images=10) with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): data, target = data.to(device), target.to(device) output = model(data) image = np.squeeze(data[0].cpu().data.numpy()) label = np.squeeze(target[0].cpu().data.numpy()) all_true.extend(target.cpu().data.numpy()) all_pred.extend(np.argmax(output.cpu().data.numpy(), axis=1)) m = torch.nn.Softmax(dim=0) for row in output.cpu(): sm = m(row) all_softmax.append(sm.data.numpy()) if i < 2: m = torch.nn.Softmax(dim=0) print("prediction percentages") print(m(output.cpu()[0])) print(all_true[i]) all_softmax.extend(m(output.cpu())) # # save sample images, or do something with output here # # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = data.shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output, target) * batch_size if outputOverlaycsv: ids = data_loader.dataset.getIds() softmax = pd.DataFrame(all_softmax) #ids = ids[:,1].reshape(ids.shape[0], 1) print(ids[0:5]) print(ids.shape) print(softmax.shape) frames = [ids, softmax, pd.DataFrame(all_true)] output_data= np.concatenate(frames, axis=1) print(output_data.shape) output_df = pd.DataFrame(output_data) output_df.to_csv('overlaycsv.csv', index=False, header=False) n_samples = len(data_loader.sampler) print("num test images = " + str(n_samples)) log = {'loss': total_loss / n_samples} log.update({met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns)}) for key in log: print("{} = {:.4f}".format(key, log[key])) #print(log) log['classes'] = classes log['test_targets'] = all_true log['test_predictions'] = all_pred print("My_metric is accuracy") util.plot_confusion_matrix(all_true, all_pred, classes=classes, normalize=False)
def main(config, args): # output_path = os.path.join(os.getcwd(), 'output-human') current_dir = os.getcwd() output_path = os.path.join(current_dir, 'output-concat') try: os.stat(output_path) except: os.mkdir(output_path) # test_list_file = args.testList # test_list = [] # test_dir = os.path.split(test_list_file)[0] # test_dir = os.path.join(test_dir, 'pic') # with open(test_list_file, 'r') as f: # test_list = f.readlines() # setup data_loader instances data_loader = getattr(module_data, config['adobe_data_loader']['type'])( "/public/Datasets/DIM-dataset/", batch_size=1, shuffle=False, validation_split=0.0, training=False, num_workers=1) #f = open(images) ## build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # get function handles of loss and metrics # loss_fn = getattr(module_loss, config['loss']) # metric_fns = [getattr(module_metric, met) for met in ["mse", "sad"]] # load state dict checkpoint = torch.load(args.resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() # total_loss = 0.0 # total_metrics = torch.zeros(len(metric_fns)) with torch.no_grad(): for i, sample_batched in enumerate(data_loader): # for item in test_list: # name_ = item.strip() # img_path = os.path.join(test_dir, name_) #img_path = sample.strip().split(' ')[0] #name_ = os.path.basename(img_path) name_ = os.path.basename(sample_batched['name'][0]) output_file_path = os.path.join(output_path, name_) # img = Image.open(img_path) # original_size = (img.height, img.width) # img_scale1 = torch.from_numpy( # np.transpose(imresize(img, (320, 320)), (2,0,1)) / 255. # ).type(torch.FloatTensor).unsqueeze(0) # img_scale1 = img_scale1.to(device) # img_scale2 = F.interpolate(img_scale1.clone(), scale_factor=0.5) # img_scale3 = F.interpolate(img_scale1.clone(), scale_factor=0.25) img_scale1 = sample_batched['image'].to(device) img_scale2 = F.interpolate(img_scale1.clone(), scale_factor=0.5) img_scale3 = F.interpolate(img_scale1.clone(), scale_factor=0.25) original_size = sample_batched['size'] # gt = sample_batched['gt'].to(device) t0 = time.time() output = model(img_scale1, img_scale2, img_scale3) print(time.time() - t0) # pred = output.data.max(1)[1].cpu().numpy() # decoded = decode_segmap(pred, 3) # decoded = np.transpose(decoded[0], (1,2,0)) # decoded = cv2.resize(decoded, (original_size[1], original_size[0]),\ # interpolation=cv2.INTER_CUBIC) # output_file_path = os.path.join(output_path, \ # os.path.basename(sample_batched['name'][0])) alpha_pred = output[0, 0, :, :].data.cpu().numpy() # alpha_pred = np.where( # alpha_pred < 0.1, # 0., # alpha_pred # ) # alpha_pred = np.where( # alpha_pred > 0.9, # 1., # alpha_pred # ) alpha_pred = cv2.resize(alpha_pred, (original_size[1], original_size[0]),\ interpolation=cv2.INTER_CUBIC) cv2.imwrite(output_file_path, alpha_pred * 255.)
def main(config, resume): # setup data_loader instances #config['data_loader']['validation_split'] = -1.0 # ensure entire test set is used train_data_loader = get_instance(module_data, 'data_loader', config) data_loader = train_data_loader.split_validation() # build model architecture model = get_instance(module_arch, 'arch', config) #model.summary() if isinstance(config['loss'], str): # do the normal thing, legacy loss_fn = getattr(module_loss, config['loss']) else: # assume loss is of dict type with same format as other classes # the loss instance should have __call__ implemented loss_fn = get_instance(module_loss, 'loss', config) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') dtype = getattr(torch, config['dtype']) # load state dict print("[TEST] Loading state_dict from checkpoint: %s" % resume) checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # ### custom code # if new_attn is not None: # del model.attention_layer #### important function to instantiate Avg3DError properly #### train data loader is only used to get some parameters #### if you run this function AFTER state_dict restore then pca weights #### for model won't be loaded but note that there can be possible mismatches #### still if pca weights in npz file doesn't match the model #### so if you do this for every new pca you would have to retrain the model or #### simply load and then do save_only. init_metrics(metric_fns, model, train_data_loader, device, dtype) # because config is loaded from test, model remains in 'train_mode' # output is low-dim output model = model.to(device, dtype) model.eval() # if getattr(model, 'train_mode', False): # model.train_mode = False # to perform De-PCA total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) batch_metrics = np.zeros(len(metric_fns)) #last_batch_size = len(data_loader)*data_loader.batch_size - len(data_loader.dataset) n_samples = len(data_loader.sampler) samples_left = n_samples #len(data_loader.dataset) # as if batch_size == 1 with torch.no_grad(): with tqdm(total=len(data_loader)) as pbar: for i, (data, target) in enumerate(data_loader): target = _tensor_to( target, device, dtype ) # no more target_dtype not required all types must be same data = _tensor_to(data, device, dtype) output = model(data) # computing loss, metrics on test set loss = loss_fn(output, target) if samples_left < data_loader.batch_size: batch_size = samples_left else: batch_size = data_loader.batch_size #print("batch_sz", batch_size) samples_left -= data_loader.batch_size total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): metric = metric(output, target) * batch_size #pbar.set_description('Metric: %0.4f' % (metric/batch_size)) batch_metrics[i] = (metric / batch_size) total_metrics[ i] += metric #metric(output, target) * batch_size pbar.set_description('Metric: %s' % np.array2string( batch_metrics, formatter={'float_kind': lambda x: "%0.2f" % x})) pbar.update(1) log = {'loss': total_loss / n_samples} log.update({ met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) }) print(log)
def main(config, resume, infile, outfile, sigma, dur, half): # build model architecture model = get_instance(module_arch, 'arch', config) model.summary() # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) if config['n_gpu'] > 1: model = model.module model.apply(remove_weight_norms) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() sr = config['arch']['args']['sr'] n_group = config['arch']['args']['n_group'] y, _ = load(infile, sr=sr, duration=dur) offset = len(y) % n_group if offset: y = y[:-offset] y = torch.Tensor(y).to(device) # get mel before turn to half, because sparse.half is not implement yet mel = model.get_mel(y[None, :]) if half: model = model.half() mel = mel.half() y = y.half() with torch.no_grad(): start = time() z, logdet, _ = model(y[None, :], mel) cost = time() - start z = z.squeeze() print(z.mean().item(), z.std().item()) print( "Forward LL:", logdet.mean().item() / z.size(0) - 0.5 * (z.pow(2).mean().item() / sigma**2 + math.log(2 * math.pi) + 2 * math.log(sigma))) print("Time cost: {:.4f}, Speed: {:.4f} kHz".format( cost, z.numel() / cost / 1000)) start = time() x, logdet = model.infer(mel, sigma) cost = time() - start print( "Backward LL:", -logdet.mean().item() / x.size(0) - 0.5 * (1 + math.log(2 * math.pi) + 2 * math.log(sigma))) print("Time cost: {:.4f}, Speed: {:.4f} kHz".format( cost, x.numel() / cost / 1000)) print(x.max().item(), x.min().item()) sf.write(outfile, x.cpu().float().numpy(), sr, subtype='PCM_16')
def main(config, resume, config_testdata=None): #PREFERENCES outputOverlaycsv = False showHeatMap = False THRESHOLD = 0.45 TRUE_CLASS = 1 PRED_CLASS = 8 # set visualization preference print("GPUs available: " + str(torch.cuda.device_count())) os.environ["CUDA_VISIBLE_DEVICES"] = "0,1" device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # setup data_loader instances data_loader = get_instance(module_data, 'data_loader_test', config_testdata) ''' data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], batch_size=512, shuffle=False, validation_split=0.0, training=False, num_workers=2 ) ''' # build model architecture #config['arch']['args']['num_feature'] = 76 model = get_instance(module_arch, 'arch', config) print(model) if torch.cuda.is_available(): print("Using GPU: " + torch.cuda.get_device_name(0)) else: print("Using CPU to test") # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) criterion = loss_fn(None) # for imbalanced datasets #criterion = loss_fn(data_loader.dataset.weight.to(device)) # for imbalanced datasets metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) #classes = ('endothelium', 'pct', 'vasculature') classes = ('s1', 's2s3', 'tal', 'dct', 'cd', 'cd45', 'nestin', 'cd31_glom', 'cd31_inter', 'cd45_1', 'cd45_2') all_pred = [] all_pred_k = [] all_true = [] all_softmax = [] thresh_true = [] thresh_pred = [] thresh_true_bad = [] thresh_pred_bad = [] below_thresh = 0 if showHeatMap: hm_layers = { 'final_layer': 'layer', 'fc_layer': 'fc_layer', 'conv_num': 17, 'fc_num': 3 } #need to set based on model heatmapper = classActivationMap.CAMgenerator(hm_layers, config, model) #heatmapper = classActivationMap.CAMgenerator3d(hm_layers, config, model) #for 3d data heatmapper.generateImage(num_images=10) with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): k = 2 data, target = data.to(device), target.to(device) #output = model(data) output = triple_prediction(data, model) #image = np.squeeze(data[0].cpu().data.numpy()) label = np.squeeze(target[0].cpu().data.numpy()) all_true.extend(target.cpu().data.numpy()) all_pred.extend(np.argmax(output.cpu().data.numpy(), axis=1)) mypred = torch.topk(output, k, dim=1)[1] all_pred_k.extend(mypred.cpu().data.numpy()) m = torch.nn.Softmax(dim=0) for i, row in enumerate(output.cpu()): sm = m(row) all_softmax.append(sm.data.numpy()) #print(np.max(sm.numpy(), axis=0)) if np.amax(sm.numpy(), axis=0) > THRESHOLD: thresh_true.append(target.cpu().data.numpy()[i]) thresh_pred.append(np.argmax(sm)) else: below_thresh += 1 thresh_true_bad.append(target.cpu().data.numpy()[i]) thresh_pred_bad.append(np.argmax(sm)) if i < 2: m = torch.nn.Softmax(dim=0) print("prediction percentages") print(m(output.cpu()[0])) print(all_true[i]) #all_softmax.extend(m(output.cpu())) #if i > 50: break # # save sample images, or do something with output here # # computing loss, metrics on test set loss = criterion(output, target) batch_size = data.shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output.cpu(), target.cpu()) * batch_size correct = 0 all_pred_k = np.array(all_pred_k) all_true_k = np.array(all_true) #CREATE 8 CLASS SOFTMAX #very ugly please fix softmax_combined = np.zeros((len(all_softmax), len(all_softmax[0]) - 3)) for i, row in enumerate(all_softmax): max_pct = np.amax([row[0], row[1]]) #can use amax instead of sum max_cd45 = np.amax([row[5], row[9], row[10]]) #can use amax instead of sum myrow = row[1:len(all_softmax[0]) - 3 + 1] myrow[0] = max_pct myrow[4] = max_cd45 softmax_combined[i] = myrow all_pred_k_combined = torch.topk( torch.from_numpy(softmax_combined), k, dim=1)[1] + 1 all_pred_k = torch.from_numpy(all_pred_k) all_true_k = torch.from_numpy(np.array(all_true)) for i in range(k): correct += torch.sum(all_pred_k[:, i] == all_true_k).item() print("TOP K all classes = {}".format(correct / len(all_pred_k))) all_pred_k = np.array(all_pred_k) all_true_k = np.array(all_true) #APPLY THRESHOLD - in development softmax_above_threshold = np.amax(softmax_combined, axis=1) softmax_above_threshold_mask = softmax_above_threshold > THRESHOLD thresh_pred_v2 = np.array(all_pred)[softmax_above_threshold_mask] thresh_true_v2 = np.array(all_true)[softmax_above_threshold_mask] #REASSIGN LABELS all_results = [ all_pred, all_true, thresh_true, thresh_pred, thresh_true_bad, thresh_pred_bad, all_pred_k, all_true_k, thresh_pred_v2, thresh_true_v2 ] all_pred, all_true, thresh_true, thresh_pred, thresh_true_bad, thresh_pred_bad, all_pred_k, all_true_k, thresh_pred_v2, thresh_true_v2 = reassign_labels( all_results, 0, 1) all_pred, all_true, thresh_true, thresh_pred, thresh_true_bad, thresh_pred_bad, all_pred_k, all_true_k, thresh_pred_v2, thresh_true_v2 = reassign_labels( all_results, 9, 5) all_pred, all_true, thresh_true, thresh_pred, thresh_true_bad, thresh_pred_bad, all_pred_k, all_true_k, thresh_pred_v2, thresh_true_v2 = reassign_labels( all_results, 10, 5) #VIEW MISTAKE CLASS interogate_CM(all_pred, all_true, TRUE_CLASS, PRED_CLASS, data_loader, classes) correct = 0 all_pred_k = torch.from_numpy(all_pred_k) all_true_k = torch.from_numpy(np.array(all_true)) for i in range(k): correct += torch.sum(all_pred_k_combined[:, i] == all_true_k).item() print("TOP K Combined classes = {}".format(correct / len(all_pred_k_combined))) # SHOW PERCENTAGE OF IMAGES BELOW THRESHOLD AS CONFUSION MATRIX cm_all = confusion_matrix(all_true, all_pred) cm_below_thresh = confusion_matrix(thresh_true_bad, thresh_pred_bad) cm_above_thresh = confusion_matrix(thresh_true, thresh_pred) compare_CM(cm_above_thresh, cm_all, classes) if outputOverlaycsv: ids = data_loader.dataset.getIds() #softmax = pd.DataFrame(all_softmax) softmax = pd.DataFrame(softmax_combined) #ids = ids[:,1].reshape(ids.shape[0], 1) print(ids[0:5]) print(ids.shape) print(softmax.shape) frames = [ids, softmax, pd.DataFrame(all_true)] output_data = np.concatenate(frames, axis=1) print(output_data.shape) output_df = pd.DataFrame(output_data) output_df.to_csv('overlaycsv.csv', index=False, header=False) n_samples = len(data_loader.sampler) print("num test images = " + str(n_samples)) log = {'loss': total_loss / n_samples} log.update({ met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) }) for key in log: print("{} = {:.4f}".format(key, log[key])) #print(log) log['classes'] = classes log['test_targets'] = all_true log['test_predictions'] = all_pred print("My_metric is accuracy") print("Number of images below threshold: {}".format(below_thresh)) print("Number of images below threshold v2: {}".format( len(all_true) - len(thresh_pred_v2))) util.plot_confusion_matrix(all_true, all_pred, classes=classes, normalize=False) util.plot_confusion_matrix(thresh_true, thresh_pred, classes=classes, normalize=False) util.plot_confusion_matrix(thresh_true_bad, thresh_pred_bad, classes=classes, normalize=False)
def main(config, resume): preview_dir = "imgs/test_png/long" ensure_dir(preview_dir) # setup data_loader instances #data_loader = getattr(module_data, config['data_loader']['type'])( # config_path="imgs/sony_png/sony_test.json", # img_dir="imgs", # batch_size=1, # shuffle=False, # validation_split=0.0, # num_workers=0 #) data_loader = get_instance(module_data, 'data_loader', config, test=True) #data_loader = data_loader.split_validation() # build model architecture model = get_instance(module_arch, 'arch', config) # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 with torch.no_grad(): inference_times = [] inference_times_copy = [] for i, (data, target) in enumerate(tqdm(data_loader)): target = target.to(device) start_with_c = time.time() data = data.to(device) start_no_c = time.time() output = model(data) end_no_c = time.time() data = data.cpu() end_with_c = time.time() inference_times.append(end_no_c - start_no_c) inference_times_copy.append(end_with_c - start_with_c) # Convert to rgb c_data = ToRGB()(torch.clamp(data.cpu(), 0, 1).numpy()[0]) c_target = ToRGB()(torch.clamp(target.cpu(), 0, 1).numpy()[0]) c_out = ToRGB()(torch.clamp(output.cpu(), 0, 1).numpy()[0]) # Save previews save_rgb_image(f"{preview_dir}/{i}_in.png", c_data) save_rgb_image(f"{preview_dir}/{i}_out.png", c_out) save_rgb_image(f"{preview_dir}/{i}_gt.png", c_target) # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = data.shape[0] total_loss += loss.item() * batch_size n_samples = len(data_loader.sampler) log = { 'loss': total_loss / n_samples, 'avg_inference_time': np.mean(inference_times), 'avg_inference_time_with_copy': np.mean(inference_times_copy) } print(log)
def main(config, resume, visual): # setup data_loader instances data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['data_dir'], batch_size=1, list_path = '1', shuffle=False, validation_split=0.0, training=False, num_workers=2, ) # build model architecture model = get_instance(Resnet, 'arch', config) print(model) # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print('devices: {}'.format(device)) model = model.to(device) print('using {}'.format(device)) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)).to(device) results = [] saving_file = 'D:/Data/Skeleton/training_result/result.npy' with torch.no_grad(): for i, (data, target) in enumerate(data_loader): data, target = data.to(device).float(), target.float().to(device) output = model(data) # if visual: # visual_mask(image, output, target, i) # # save sample images, or do something with output here # # computing loss, metrics on test set if i % 20 == 0: print('inference [{} / {}]'.format(i, len(data_loader))) results.append(np.array(output.to('cpu'))) # loss = loss_fn(output, target) # batch_size = data.shape[0] # total_loss += loss.item() * batch_size # for i, metric in enumerate(metric_fns): # total_metrics[i] += metric(output, target) * batch_size # with open(saving_file, 'w+') as f: # for r in results: # f.write("%s\n" % r) np.save(saving_file, np.array(results)) print('result saving to {}'.format(saving_file))
def main(config, resume, target_class): # setup data_loader instances data_loader = getattr(module_data, config['testloader']['type'])( config['testloader']['args']['data_dir'], config['testloader']['args']['file'], batch_size=32, shuffle=True, validation_split=0.0, input_size=config['testloader']['args']['input_size'], num_workers=4 ) # build model architecture if config['arch']['type'] == 'InceptionV3': model_instance = get_instance(module_arch_inception, 'arch', config) if config['arch']['type'] == 'VGG19Model': model_instance = get_instance(module_arch_vgg, 'arch', config) if config['arch']['type'] == 'AlexNet': model_instance = get_instance(module_arch_alexnet, 'arch', config) if config['arch']['type'] == 'Net': model = get_instance(module_arch_testnet, 'arch', config) if config['arch']['type'] != 'Net': #model = model_instance.build_model(config['arch']['args']['model_path']) model = model_instance.build_model('../../model') # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] # load state dict checkpoint = torch.load(resume, map_location='cpu') state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing dev = 'cuda:'+str(config['gpu_id']) device = torch.device(dev if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) y_pred = torch.tensor([],dtype=torch.long) y_true = torch.tensor([],dtype=torch.long) whatIsThis = data_loader with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): data, target = data.to(device), target.to(device) output = model(data) pred = torch.argmax(output, dim=1) y_pred = torch.cat((y_pred, pred.cpu())) y_true = torch.cat((y_true, target.cpu())) # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = data.shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output, target) * batch_size n_samples = len(data_loader.sampler) log = {'loss': total_loss / n_samples} log.update({met.__name__ : total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns)}) if target_class == None: target_names = [str(i) for i in range(0,config['arch']['args']['classes'])] else: target_names = target_class['targets'] cl_report = classification_report(y_true, y_pred, target_names=target_names) #save_dir = str('results/'+args.resume.split('/')[1]+'/'+args.resume.split('/')[2]) save_dir = str('results/' + args.resume.split('/')[1]) ensure_dir(save_dir) file_name = os.path.join(save_dir, 'classification_report.txt') accuracy = torch.sum(y_pred == y_true).item() / len(y_true) print('\nAccuracy of the model : {}'.format(round(100*accuracy, 2))) with open(file_name,'w') as fh: fh.writelines('Accuracy of the model : {}\n\n'.format(round(100*accuracy, 2))) fh.write('Classification report : \n\n') fh.writelines(cl_report)
def main(config, resume): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print("GPUs available: " + str(torch.cuda.device_count())) os.environ["CUDA_VISIBLE_DEVICES"] = "0,1" #data_loader = get_instance(module_data, 'data_loader', config) --> cant use this because it will auto split a validation set data_loader = hdf5_3d_dataloader( config['data_loader']['args']['hdf5_path'], batch_size=128, shuffle=False, validation_split=0.0, training=True, mean=config['data_loader']['args']['mean'], stdev=config['data_loader']['args']['stdev']) print(len(data_loader.dataset)) data_loader_test = get_instance(module_data, 'data_loader_test', config) # Load trained model, including fully connected model = get_instance(module_arch, 'arch', config) print(model) if torch.cuda.is_available(): print("Using GPU: " + torch.cuda.get_device_name(0)) else: print("Using CPU to test") loss_fn = getattr(module_loss, config['loss']) criterion = loss_fn(None) # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) model = model.to(device) model.eval() classes = ('S1', 'PCT', 'TAL', 'DCT', 'CD', 'CD45', 'Nestin', 'CD31_glom', 'CD31_inter') #identify feature layer to separate save_output = SaveOutput() #print(model.module.conv_layer4) handle = model.module.fc6.register_forward_hook(save_output) #generate feature set all_true = [] feature_set = [] with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader)): data, target = data.to(device), target.to(device) output = model(data) all_true.extend(target.cpu().data.numpy()) print(i * 128) all_true = np.array(all_true) all_features = np.array(save_output.outputs) for item in all_features: feature_set.extend(item) all_features = np.array(feature_set) #all_features = np.reshape(all_features, (feature_shape[0]*feature_shape[1], feature_shape[2])) print(all_true.shape) print(all_features.shape) #create + train logistic model minmaxscaler = MinMaxScaler() all_features_scaled = minmaxscaler.fit_transform(all_features) clf = LogisticRegression(random_state=0, class_weight='balanced', verbose=1, solver='lbfgs', n_jobs=-1).fit(all_features_scaled, all_true) #solver options: saga, lbfgs #test logistic model save_output_test = SaveOutput() handle = model.module.fc6.register_forward_hook(save_output_test) all_true_test = [] feature_set_test = [] all_softmax_cnn = [] with torch.no_grad(): for i, (data, target) in enumerate(tqdm(data_loader_test)): data, target = data.to(device), target.to(device) output = model(data) all_true_test.extend(target.cpu().data.numpy()) #all_pred_cnn.extend(np.argmax(output.cpu().data.numpy(), axis=1)) m = torch.nn.Softmax(dim=0) for i, row in enumerate(output.cpu()): sm = m(row) all_softmax_cnn.append(sm.data.numpy()) all_true_test = np.array(all_true_test) all_features_test = np.array(save_output_test.outputs) for item in all_features_test: feature_set_test.extend(item) all_features_test = np.array(feature_set_test) print(all_true_test.shape) print(all_features_test.shape) all_pred = clf.predict(minmaxscaler.transform(all_features_test)) all_softmax_lr = clf.predict_proba(all_features_test) all_softmax_combined = all_softmax_lr + all_softmax_cnn all_pred_combined = np.argmax(all_softmax_combined, axis=1) #REASSIGN LABELS all_results = [all_pred, all_true_test, all_pred_combined] all_pred, all_true_test, all_pred_combined = reassign_labels( all_results, 0, 1) all_pred, all_true_test, all_pred_combined = reassign_labels( all_results, 9, 5) all_pred, all_true_test, all_pred_combined = reassign_labels( all_results, 10, 5) #display results util.plot_confusion_matrix(all_true_test, all_pred, classes=classes, normalize=False) #log regression util.plot_confusion_matrix( all_true_test, all_pred_combined, classes=classes, normalize=False) #average of log regression and cnn
def main(config, resume, env): # build model architecture config['arch']['args']['batch_size'] = 1 model = get_instance(module_arch, 'arch', config) model.summary() # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() # initialize gym env num_rollouts = 5 env = gym.make(env) max_steps = env.spec.timestep_limit # loop for num_rollouts returns = [] for i in range(num_rollouts): print('Iter', i) steps = 0 totalr = 0.0 done = False obs = env.reset() if config['arch']['mode'] == 'recurrent': model.hidden = model.init_hidden(1) while not done: # predict action if config['arch']['mode'] == 'recurrent': obs = obs[None, None, :] else: obs = obs[None, :] obs = torch.from_numpy(obs.astype(np.float32)) if torch.cuda.is_available(): obs = obs.cuda() action = model(Variable(obs)) # perform step in env action = action.data if torch.cuda.is_available(): action = action.cpu() action = action.numpy() if config['arch']['mode'] == 'recurrent': action = action[0,0,:] else: action = action[0,:] obs, r, done, _ = env.step(action) # update stats steps += 1 totalr += r env.render() # break condition if steps >= max_steps: break returns.append(totalr) # output statistics print('returns', returns) print('mean return', np.mean(returns)) print('std of return', np.std(returns))
def main(config, resume): # setup data_loader instances data_loader = getattr(module_data, config['data_loader']['type'])( config['data_loader']['args']['test_filename'], batch_size=config['data_loader']['args']['batch_size'], shuffle=False, validation_split=0.0, training=False, num_workers=0, test_filename='hackyPlaceholder') # build model architecture model = get_instance(module_arch, 'arch', config) print(model) # Show how many parameters model_parameters = filter(lambda p: p.requires_grad, model.parameters()) params = sum([np.prod(p.size()) for p in model_parameters]) print("Number of trainable parameters:" + str(params)) # get function handles of loss and metrics loss_fn = getattr(module_loss, config['loss']) metric_fns = [getattr(module_metric, met) for met in config['metrics']] metric_fns.append(module_metric.balanced_acc) metric_fns.append(module_metric.precision) # load state dict checkpoint = torch.load(resume) state_dict = checkpoint['state_dict'] if config['n_gpu'] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = model.to(device) model.eval() total_loss = 0.0 total_metrics = torch.zeros(len(metric_fns)) with torch.no_grad(): for (data, target, lengths) in tqdm(data_loader): data, target, lengths = move_to_device(data, target, lengths, device) output = model(data, lengths) # # save sample images, or do something with output here # # computing loss, metrics on test set loss = loss_fn(output, target) batch_size = data[0].shape[0] total_loss += loss.item() * batch_size for i, metric in enumerate(metric_fns): total_metrics[i] += metric(output, target) * batch_size n_samples = len(data_loader.sampler) log = {'loss': total_loss / n_samples} log.update({ met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) }) print(log)
def main(config: dict, resume: Optional[str]): # Instantiate data loader. data_loader: DataLoader = getattr( module_data, config["data_loader"]["type"])( config["data_loader"]["args"]["data_dir"], batch_size=512, shuffle=False, validation_split=0.0, training=False, num_workers=2) # Instantiate model and print summary. model: Module = get_instance(module_arch, "arch", config, data_loader.dataset.feats) model.summary() # Obtain function handles of loss and metrics. loss_fn: Callable = getattr(module_loss, config["loss"]["type"]) loss_args: dict = config["loss"]["args"] metric_fns: List[Callable] = [ getattr(module_metric, met) for met in config["metrics"] ] metric_args: List[dict] = [ config["metrics"][met] for met in config["metrics"] ] # Load state dict. checkpoint: dict = torch.load(resume) state_dict: dict = checkpoint["state_dict"] if config["n_gpu"] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # Prepare model for testing. device: str = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) model.eval() total_loss: float = 0.0 total_metrics: torch.Tensor = torch.zeros(len(metric_fns)) with torch.no_grad(): i: int data: torch.Tensor target: torch.Tensor for i, (data, target) in enumerate(tqdm(data_loader)): data, target = data.to(device), target.to(device) output: torch.Tensor = model(data) # # save sample images, or do something with output here # # computing loss, metrics on test set loss: torch.Tensor = loss_fn(output, target, **loss_args) batch_size: int = data.shape[0] total_loss += loss.item() * batch_size j: int metric: Callable for j, metric in enumerate(metric_fns): if metric.__name__ == "adj_rsqr": total_metrics[j] += metric(output, target, data.shape[1], **metric_args[j]) * batch_size else: total_metrics[j] += metric(output, target, ** metric_args[j]) * batch_size n_samples: int = len(data_loader.sampler) log: dict = {"loss": total_loss / n_samples} log.update({ met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns) }) print(log)
def main(config, args): torch.set_default_tensor_type("torch.cuda.FloatTensor") # setup data_loader instances word2id, word2vec = load_word_dict(os.path.join(args.input, "word_vec.json"), anony=False) rel2id = load_rel_dict(os.path.join(args.input, "rel2id.json")) # build model architecture model = get_instance(module_arch, "arch", config, word_vec_mat=word2vec, relation_num=len(rel2id)) print(model) # get function handles of metrics metric_fns = [ getattr(module_metric, met) for met in config["eval_metrics"] ] # load state dict checkpoint = torch.load(args.resume) state_dict = checkpoint["state_dict"] if config["n_gpu"] > 1: model = torch.nn.DataParallel(model) model.load_state_dict(state_dict) # prepare model for testing device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) model.eval() save = args.save # setup data_loader instances data_dir = args.input test_data_loader = module_data.BaseNytLoader( data_dir, word2id, rel2id, 120, 2000, validation_split=0, mode="test", src="test", method=config["data_loader"]["args"]["method"] if args.method is None else args.method, batch_type=1, select=0, filtering_mode=0, shuffle=False, num_workers=1, anonymization=False, ) print("%d entities pairs in total" % len(test_data_loader.subset)) total_output = [] total_target = [] with torch.no_grad(): for i, (data, target) in enumerate(tqdm(test_data_loader)): data, target = ( assign_to_device(data, device), assign_to_device(target, device), ) output = model(data, is_train=False) total_output.append(output) total_target.append(target) total_output = torch.cat(total_output) total_target = torch.cat(total_target) total_metrics = np.zeros(len(metric_fns)) for i, metric in enumerate(metric_fns): total_metrics[i], _ = metric(total_output, total_target, is_train=False, save=save) log = {} log.update({ met.__name__: total_metrics[i].item() for i, met in enumerate(metric_fns) }) for metric_name, metric_value in log.items(): print("%s: %.4f" % (metric_name, metric_value))