def _infer(model, root_path, test_loader=None): if test_loader is None: test_loader = data_loader(root=os.path.join(root_path, 'test_data'), phase='test') model.eval() res_fc = None res_id = None for idx, (data_id, image, _) in enumerate(tqdm(test_loader)): image = image.cuda() with torch.no_grad(): fc = model(image) fc = fc.detach().cpu().numpy() fc = np_softmax(fc) # with torch.no_grad(): # fc2 = model(torch.flip(image, (3, ))) # TTA : horizontal flip # fc2 = fc2.detach().cpu().numpy() # fc2 = np_softmax(fc2) # fc = fc + fc2 if C.get()['infer_mode'] == 'face': fc[:, range(60)] = -1 # target_lb = list(range(60, 100)) if idx == 0: res_fc = fc res_id = data_id else: res_fc = np.concatenate((res_fc, fc), axis=0) res_id = res_id + data_id res_cls = np.argmax(res_fc, axis=1) return [res_id, res_cls]
def _infer(model, root_path, test_loader=None): if test_loader is None: test_loader = data_loader( root=os.path.join(root_path, 'test_data'), phase='test') x_hats = [] fnames = [] desc = 'infer...' with torch.no_grad(): for data in tqdm(test_loader, desc=desc, total=len(test_loader), disable=use_nsml): if isinstance(test_loader.dataset, torch.utils.data.dataset.Subset): fname, x_input, mask, _ = data else: fname, x_input, mask = data x_input = x_input.cuda() mask = mask.cuda() x_mask = torch.cat([x_input, mask], dim=1) x_hat = model(x_mask) x_hat = compose(x_input, x_hat, mask) x_hats.append(x_hat.cpu()) fnames = fnames + list(fname) x_hats = torch.cat(x_hats, dim=0) return fnames, x_hats
def _infer(model, root_path, test_loader=None): if test_loader is None: test_loader = data_loader(root=os.path.join(root_path, 'test_data'), phase='test') feats = None data_ids = None s_t = time.time() for idx, (data_id, image, _) in enumerate(test_loader): image = image.cuda() feat = model(image, extract=True) feat = feat.detach().cpu().numpy() feat = feat / np.linalg.norm(feat, axis=1)[:, np.newaxis] if feats is None: feats = feat else: feats = np.append(feats, feat, axis=0) if data_ids is None: data_ids = data_id else: data_ids = np.append(data_ids, data_id, axis=0) if time.time() - s_t > 10: print('Infer batch {}/{}.'.format(idx + 1, len(test_loader))) score_matrix = feats.dot(feats.T) np.fill_diagonal(score_matrix, -np.inf) top1_reference_indices = np.argmax(score_matrix, axis=1) top1_reference_ids = [[ data_ids[idx], data_ids[top1_reference_indices[idx]] ] for idx in range(len(data_ids))] return top1_reference_ids
def _infer(model, root_path, test_loader=None): if test_loader is None: test_loader = data_loader(root=os.path.join(root_path, 'test_data'), phase='test') ensembles_xy = [] ensembles_w = [] for sess, chkp, w in archives: nsml.load(checkpoint=chkp, session=sess) model.eval() outputs = [] outputs_w = [] num_data = 0 for idx, (image, _) in enumerate(test_loader): with torch.no_grad(): locs, scores = model(image.cuda()) all_images_boxes, all_scores = model.detect_objects( locs, scores) for box in all_images_boxes: box = box.detach().cpu().numpy() box_xy = np.array( [box[0], box[1], box[0] + box[2], box[1] + box[3]], dtype=np.float32) outputs.append(box_xy) outputs_w.extend(all_scores) num_data += len(all_images_boxes) ensembles_xy.append(np.array(outputs)) ensembles_w.append(outputs_w) # ensembles_xy = np.mean(ensembles_xy, axis=0) ensemble_result = [None] * len(ensembles_xy[0]) best_w = defaultdict(lambda: 0) for xys, ws in zip(ensembles_xy, ensembles_w): for i, (xy, w) in enumerate(zip(xys, ws)): if best_w[i] > w: continue ensemble_result[i] = xy best_w[i] = w ensembles_xy = np.array(ensemble_result) print(ensembles_xy.shape) assert ensembles_xy.shape[0] == num_data assert ensembles_xy.shape[1] == 4 ensembles = [] for xy in ensembles_xy: box = np.array([xy[0], xy[1], xy[2] - xy[0], xy[3] - xy[1]]) ensembles.append(box) outputs = np.stack(ensembles, axis=0) assert outputs.shape[0] == num_data assert outputs.shape[1] == 4 print(outputs.shape) return outputs
def _infer(model, root_path, test_loader=None): """Inference function for NSML infer. Args: model: Trained model instance for inference root_path: Set proper path for local evaluation. test_loader: Data loader is defined in `data_local_loader.py`. Returns: results: tuple of (image_names, outputs) image_names: list of file names (size: N) (ex: ['aaaa.jpg', 'bbbb.jpg', ... ]) outputs: numpy array of bounding boxes (size: N x 4) (ex: [[x1,y1,x2,y2],[x1,y1,x2,y2],...]) """ if test_loader is None: # Eval using local dataset loader test_loader = data_loader( root=os.path.join(root_path, 'test_data'), phase='test') model.eval() outputs = [] image_names = [] s_t = time.time() for idx, (img_names, image) in enumerate(test_loader): image = image.cuda() output = model(image) bbox = output[:, :4] conf = output[:, 4] bbox = bbox.sigmoid() conf = conf.sigmoid() bbox_valid = bbox bbox_valid = bbox_valid.detach().cpu().numpy() img_names = np.asarray(img_names) img_names_valid = img_names # [IMPORTANT] # Convert bbox coords to original image scale (960 * 540) # Evaluation metric is computed in original image scale. bbox_valid[:, 0] *= IMG_WIDTH bbox_valid[:, 1] *= IMG_HEIGHT bbox_valid[:, 2] *= IMG_WIDTH bbox_valid[:, 3] *= IMG_HEIGHT outputs.append(bbox_valid.astype(np.int16)) image_names += list(img_names_valid) if time.time() - s_t > 10: print('Infer batch {}/{}.'.format(idx + 1, len(test_loader))) outputs = np.concatenate(outputs, 0) results = (image_names, outputs) return results
def _infer(model, root_path, test_loader=None): if test_loader is None: test_loader = data_loader(root=os.path.join(root_path, 'test_data'), phase='test') outputs = [] s_t = time.time() for idx, (image, _) in enumerate(test_loader): image = image.cuda() output = model(image) output = output.detach().cpu().numpy() outputs.append(output) if time.time() - s_t > 10: print('Infer batch {}/{}.'.format(idx + 1, len(test_loader))) outputs = np.concatenate(outputs, 0) return outputs
def _infer(model, root_path, test_loader=None): if test_loader is None: test_loader = data_loader(root=os.path.join(root_path, 'test_data'), phase='test') model.eval() outputs = [] for idx, (image, _) in enumerate(test_loader): with torch.no_grad(): locs, scores = model(image.cuda()) all_images_boxes, _ = model.detect_objects(locs, scores) for box in all_images_boxes: box = box.detach().cpu().numpy() outputs.append(box) outputs = np.stack(outputs, axis=0) return outputs
def _infer(model, root_path, test_loader=None): if test_loader is None: test_loader = data_loader(root=os.path.join(root_path, 'test_data'), phase='test') res_fc = None res_id = None for idx, (data_id, image, _) in enumerate(test_loader): image = image.cuda() fc = model(image, extract=True) fc = fc.detach().cpu().numpy() if idx == 0: res_fc = fc res_id = data_id else: res_fc = np.concatenate((res_fc, fc), axis=0) res_id = res_id + data_id res_cls = np.argmax(res_fc, axis=1) return [res_id, res_cls]
def _infer(model, root_path, loader=None): """Local inference function for NSML infer. Args: model: instance. Any model is available. root_path: string. Automatically set by NSML. test_loader: instance. Data loader is defined in `data_local_loader.py`. Returns: predictions_str: list of string. ['img_1,1,0,1,0,1,0,0,0', 'img_2,0,1,0,0,1,0,0,0', ...] """ model.eval() if loader is None: loader = data_loader(root=os.path.join(root_path)) list_of_fids = [] list_of_preds = [] for idx, (image, fid, _) in enumerate(loader): image = image.cuda() fc = model(image, extract=True) fc = fc.detach().cpu().numpy() fc = 1 * (fc > 0.5) list_of_fids.extend(fid) list_of_preds.extend(fc) predictions_str = [] for idx, fid in enumerate(list_of_fids): test_str = fid for pred in list_of_preds[idx]: test_str += ',{}'.format(pred) predictions_str.append(test_str) return predictions_str
[param for param in model.parameters() if param.requires_grad], lr=config.base_lr, weight_decay=1e-2) scheduler = ReduceLROnPlateau(optimizer) if IS_ON_NSML: # This NSML block is mandatory. Do not change. bind_nsml(model) nsml.save('checkpoint') if config.pause: nsml.paused(scope=locals()) if config.mode == 'train': # Local debugging block. This module is not mandatory. # But this would be quite useful for troubleshooting. train_loader = data_loader(root=DATASET_PATH, split='train') val_loader = data_loader(root=DATASET_PATH, split='val') num_batches = len(train_loader) best_accuracy = 0.0 for epoch in range(config.num_epochs): model.train() total_loss = 0.0 num_images = 0 for iter_, (image, image_id, label) in enumerate(train_loader): image = image.cuda() label = label.cuda() pred = model(image) loss = criterion(pred, label)
# These three arguments are reserved for nsml. Do not change. args.add_argument("--mode", type=str, default="train") args.add_argument("--iteration", type=str, default='0') args.add_argument("--pause", type=int, default=0) config = args.parse_args() num_classes = config.num_classes cuda = config.cuda mode = config.mode model = ClsResNet(models.resnet.BasicBlock, [2, 2, 2, 2], num_classes) load_weight(model) if cuda: model = model.cuda() if IS_ON_NSML: # This NSML block is mandatory. Do not change. bind_nsml(model) nsml.save('checkpoint') if config.pause: nsml.paused(scope=locals()) if mode == 'train': # Local debugging block. This module is not mandatory. # But this would be quite useful for troubleshooting. gt_label = os.path.join(DATASET_PATH, 'test_label') loader = data_loader(root=DATASET_PATH, batch_size=64) result = local_eval(model, loader, gt_label)