# Forward + Backward Prop optimizer.zero_grad() torch.set_grad_enabled(True) outputs = model.forward(inputs) predictions = torch.max(outputs, 1)[1] loss = criterion(outputs, labels.long().squeeze(1)) loss.backward() optimizer.step() # statistics running_loss += loss.item() writer.add_scalar('data/Train BatchWise Loss', loss.item(), total_iter_num) _total_iou, per_class_iou, num_images_per_class = utils.get_iou( predictions, labels, n_classes=config.train.numClasses) total_iou += _total_iou # Print loss every 20 Batches # if (iter_num % 20) == 0: # print('Epoch{} Batch{} BatchLoss: {:.4f} '.format(epoch, iter_num, loss.item())) # Log Epoch Loss epoch_loss = running_loss / (len(trainLoader)) writer.add_scalar('data/Train Epoch Loss', epoch_loss, total_iter_num) print('\nTrain Epoch Loss: {:.4f}'.format(epoch_loss)) # Log mIoU miou = total_iou / ((len(trainLoader)) * config.train.batchSize) writer.add_scalar('data/Train mIoU', miou, total_iter_num) print('Train mIoU: {:.4f}'.format(miou))
total_iou = 0.0 for ii, sample_batched in enumerate(tqdm(testLoader)): inputs, labels = sample_batched # Forward pass of the mini-batch inputs = inputs.to(device) labels = labels.to(device) with torch.no_grad(): outputs = model(inputs) predictions = torch.max(outputs, 1)[1] _total_iou, per_class_iou, num_images_per_class = utils.get_iou( predictions, labels.long().squeeze(1), n_classes=config.train.numClasses) total_iou += _total_iou # Save output images, one at a time, to results img_tensor = inputs.detach().cpu() output_tensor = outputs.detach().cpu() label_tensor = labels.detach().cpu() # Extract each tensor within batch and save results for iii, sample_batched in enumerate( zip(img_tensor, output_tensor, label_tensor)): image, output, label = sample_batched result_path = os.path.join( results_dir, SUBDIR_RESULT,
def _ocr_extend_dataset(self, data, path): paths = glob.glob(f'{path}/**/ocr_output.json', recursive=True) for path_ in paths: with open(path_, "r") as f: _data = json.load(f) name = Path(path_).parent.stem # load ground truth gt_name = f"{name}.json" if gt_name not in data: continue gt_dict = data[gt_name] data_key = f"ocr_extend_{name}.json" texts = [] bboxes_for_train = [] input_bboxes = [] keys_type = [] formal_keys = [] match = 0 not_match = 0 # _data is list for label in _data: text = str(label["text"]) text = text.replace('\n', '').replace(' ', '') match_index = -1 match_value = 0 now_box_list = [*label["location"][0], *label["location"][2]] now_box = { key: value for key, value in zip(["x1", "y1", "x2", "y2"], now_box_list) } for i, bbox_for_train in enumerate(gt_dict["bboxes"]): test_box = { key: value for key, value in zip(["x1", "y1", "x2", "y2"], bbox_for_train) } # LOGGER.info(now_box, test_box) iou = get_iou(now_box, test_box) if iou > 0.5 and iou > match_value: match_index = i match_value = iou if match_index != -1: keys_type.append(gt_dict["keys_type"][match_index]) formal_keys.append(gt_dict["formal_keys"][match_index]) else: # LOGGER.info("didn't find match") keys_type.append("") formal_keys.append("") # construct the graph input_bbox_format, bbox_for_train = [ *now_box_list, 1, 1, text, None ], now_box_list texts.append(text) bboxes_for_train.append(bbox_for_train) input_bboxes.append(input_bbox_format) data[data_key]["image_file"] = gt_dict["image_file"] data[data_key]["keys_type"] = keys_type data[data_key]["formal_keys"] = formal_keys data[data_key]["category"] = gt_dict["category"] # data[key]["text"] = "".join(texts) data[data_key]["texts"] = texts data[data_key]["bboxes"] = bboxes_for_train data[data_key]["input_bboxes"] = input_bboxes