<jupyter_start><jupyter_text>Printing Truth Labels and Images<jupyter_code>from detectron2.data.detection_utils import *
annotations = pd.DataFrame.from_dict(train_json)["annotations"].explode().apply(pd.Series)
annotations_val = pd.DataFrame.from_dict(val_json)["annotations"].explode().apply(pd.Series)
np.unique(annotations_val['category_id'])
for d in random.sample(train_json, 1):
    if len(d['annotations']) > 0:
        img = imread(d["file_name"])
        img = img[:, :, ::-1]
#         normalized = max_norm_channels(np.where(img < 0, 0, img))
#         rescaled = rescale_intensity(normalized, out_range=(0,255))
#         masked = np.where(rescaled==0, np.nan, rescaled)
        visualizer = Visualizer(img, metadata={}, scale=1)
        vis = visualizer.draw_dataset_dict(d)
        plt.figure(figsize = (4,8))
        imshow(vis.get_image())<jupyter_output><empty_output><jupyter_text>Model Inspection Something to check, there is a predicted category "2" even though we only have 1 category, "1". in the dataset. might be because of the config expectign multiple COCO classes? need to look at shape of last layer of the model in pytorch somehow. after looking at the number of categorie sint he val dataset that faile dto run it looks like the correct num of categories, 2 is present which means detectron2 is probably expecting that there should be 81 categories becuase the model head was not reset because 81 was kept in the config.<jupyter_code>cfg.MODEL.WEIGHTS = os.path.join("/datadrive/cropmask_experiments/denmark-smallanchorscales-long/", "model_0003799.pth")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.3  # set the testing threshold for this model
predictor = DefaultPredictor(cfg)
weights = torch.load(cfg.MODEL.WEIGHTS)

[key for key in weights['model'].keys() if "backbone" not in key]
neb_weights = torch.load(os.path.join("/datadrive/cropmask_experiments/nirrg-nms7/", "model_0002399.pth"))<jupyter_output><empty_output><jupyter_text>looks like wrong weights for denmark model, correct weights for cropmask model.<jupyter_code>neb_weights['model']['roi_heads.mask_head.predictor.weight'].shape
weights['model']['roi_heads.mask_head.predictor.weight'].shape
from detectron2.utils.visualizer import ColorMode
for d in random.sample(train_json, 1):    
    im = imread(d["file_name"])
    outputs = predictor(im)
    vis_im = make_vis_im(im)
    visualizer_preds = Visualizer(vis_im,
                   scale=1, 
                   metadata= {}   # remove the colors of unsegmented pixels
    )
    visualizer_labels = Visualizer(vis_im,
                   scale=1, 
                   metadata= {}   # remove the colors of unsegmented pixels
    )
    v = visualizer_preds.draw_instance_predictions(outputs["instances"].to("cpu"))
    labels = visualizer_labels.draw_dataset_dict(d)
    fig, ax = plt.subplots(nrows=1, ncols=2, figsize = (16,16))
    imshow(v.get_image(), ax=ax[0])
    imshow(labels.get_image(), ax=ax[1])
from detectron2.evaluation import COCOEvaluator, inference_on_dataset
from detectron2.data import build_detection_test_loader
evaluator = COCOEvaluator("validation", cfg, False, output_dir="/datadrive/evaluator_output/")
test_loader = build_detection_test_loader(cfg, cfg.DATASETS.VALIDATION[0])
inference_on_dataset(predictor.model, test_loader, evaluator)
# another equivalent way is to use trainer.test
import torch
torch.load("/home/ryan/InstanceSegmentation_Sentinel2/model/resnet_v1_101-0000.params")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11063341931785611, 'fraction_numerical': 0.01834336486099169, 'mean_word_length': 4.787728026533997, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '478622', 'license': 'MIT', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1120, 'n_words': 262, 'path': 'fcis/InstanceSegmentation_Sentinel2/model_training.ipynb', 'repo_name': 'ecohydro/CropMask_RCNN', 'size': '3489'}