num_workers=args.workers, pin_memory=True, sampler=RangeSampler(args.range)) #Load training data hardModel = ResNet18(num_classes=200) hardModel = HardNBDT(pretrained=True, dataset='TinyImagenet200', arch='ResNet18', model=hardModel) hardModel = hardModel.cuda() softModel = ResNet18(num_classes=200) softModel = SoftNBDT(pretrained=True, dataset='TinyImagenet200', arch='ResNet18', model=softModel) softModel = softModel.cuda() explainer = Explainer(hardModel, (64, 64), 500) explainer.generate_masks(1000, 8, 0.1, 'temp.npy') klen = 11 ksig = 5 kern = gkern(klen, ksig) blur = lambda x: nn.functional.conv2d(x, kern, padding=klen // 2) insertion = CausalMetric(softModel, 'ins', 64 * 8,
batch_size=batch_size, shuffle=False, num_workers=1, pin_memory=True, sampler=RangeSampler(range_sample)) hardModel = wrn28_10_cifar10() hardModel = HardNBDT(pretrained=True, dataset='CIFAR10', arch='wrn28_10_cifar10', model=hardModel) hardModel = hardModel.cuda() softModel = wrn28_10_cifar10() softModel = SoftNBDT(pretrained=True, dataset='CIFAR10', arch='wrn28_10_cifar10', model=softModel) softModel = softModel.cuda() explainer = Explainer(hardModel, (32, 32), 500) explainer.generate_masks(1000, 8, 0.1, 'temp.npy') klen = 11 ksig = 5 kern = gkern(klen, ksig) blur = lambda x: nn.functional.conv2d(x, kern, padding=klen // 2) insertion = CausalMetric(softModel, 'ins', 32 * 8,
import streamlit as st import numpy as np import os from nbdt.model import SoftNBDT from nbdt.models import ResNet18, wrn28_10_cifar10, wrn28_10_cifar100, wrn28_10 # use wrn28_10 for TinyImagenet200 from torchvision import transforms from nbdt.utils import DATASET_TO_CLASSES, load_image_from_path, maybe_install_wordnet from IPython.display import display model = wrn28_10_cifar10() model = SoftNBDT(pretrained=True, dataset='CIFAR10', arch='wrn28_10_cifar10', model=model, hierarchy='wordnet') transforms = transforms.Compose([ transforms.Resize(32), transforms.CenterCrop(32), transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), ]) ############### streamlit stuff ############### def file_selector(folder_path='.'): filenames = os.listdir(folder_path) selected_filename = st.selectbox('Select a file', filenames) return os.path.join(folder_path, selected_filename)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2) Colors.cyan(f'Training with dataset {args.dataset} and {len(trainset.classes)} classes') # Model print('==> Building model..') model = getattr(models, args.arch) model_kwargs = {'num_classes': len(trainset.classes) } if args.pretrained: print('==> Loading pretrained model..') net = model(pretrained=True, dataset=args.dataset, **model_kwargs) else: print('==> Loading NBDT model..') net = ResNet18() net = SoftNBDT(pretrained=True, dataset='CIFAR10', arch='ResNet18', model=net) #Analyzer class_analysis = getattr(analysis, args.analysis or 'Noop') analyzer_kwargs = generate_kwargs(args, class_analysis, name=f'Analyzer {args.analysis}', keys=analysis.keys, globals=globals()) analyzer = class_analysis(**analyzer_kwargs) #Criterion criterion = nn.CrossEntropyLoss() class_criterion = getattr(loss, args.loss) loss_kwargs = generate_kwargs(args, class_criterion, name=f'Loss {args.loss}', keys=loss.keys,
import streamlit as st import numpy as np import os from nbdt.model import SoftNBDT from nbdt.models import ResNet18, wrn28_10_cifar10, wrn28_10_cifar100, wrn28_10 # use wrn28_10 for TinyImagenet200 from torchvision import transforms from nbdt.utils import DATASET_TO_CLASSES, load_image_from_path, maybe_install_wordnet from IPython.display import display import base64 model = wrn28_10_cifar10() model = SoftNBDT( pretrained=True, dataset='CIFAR10', arch='wrn28_10_cifar10', model=model, hierarchy='wordnet') def set_bg_hack(): # set bg name main_bg = "nbdt_bg.png" main_bg_ext = "png" # we can add a side bg if necessary # side_bg = "sample.jpg" # side_bg_ext = "jpg" st.markdown( f"""
def test_nbdt_soft_cifar10(input_cifar10, resnet18_cifar10): model_soft = SoftNBDT(dataset="CIFAR10", model=resnet18_cifar10, hierarchy="induced") model_soft(input_cifar10)
def test_nbdt_soft_tinyimagenet200(input_tinyimagenet200, resnet18_tinyimagenet200): model_soft = SoftNBDT(dataset="TinyImagenet200", model=resnet18_tinyimagenet200, hierarchy="induced") model_soft(input_tinyimagenet200)