Package: fastai2-extensions
FastAI2 ExtensionsThis library is a collection of utility functions for a variety of purposes that fit right into thefastai2ecosystem. It's broadly divided into 3 modules --interpret,augment, andinference.Installpip install fastai2_extensionsInterpretationClassificationInterpretationExExtends fastai'sClassificationInterpretationto plot model confidence and per-label accuracy bar graphs. It also adds some convenience to grab filenames based on these confidence levels.This part of the library is currently suitable for Softmax classifiers only. Multilabel support will be added soon.fromfastai2.vision.allimport*fromfastai2_extensions.interpret.allimport*learn=load_learner('/Users/rahulsomani/Desktop/shot-lighting-cast/fastai2-110-epoch-model.pkl')interp=ClassificationInterpretationEx.from_learner(learn)plt.style.use('ggplot')interp.plot_accuracy()interp.plot_label_confidence()GradCamTheGradCamobject takes in 3 args:learn: a fastaiLearnerfname: path to the image file to draw the heatcam overlabels: list of labels to draw the heatmap for. IfNone, draws for the highest predicted classThere's quite a few plotting options. For more options, see the docs.importPILfname='../assets/imgs/alice-in-wonderland.jpg'PIL.Image.open(fname).resize((550,270))gcam=GradCam(learn,fname,None)gcam.plot(full_size=True,plot_original=True,figsize=(12,6))gcam=GradCam(learn,fname,['shot_lighting_cast_hard','shot_lighting_cast_soft'])gcam.plot(full_size=False,plot_original=False,figsize=(12,4))Comparing Multiple Modelscompare_vennlets you compares 2 or models trained evaluated on the same dataset to inspect model agreement. If you only input 2 or 3 models, then you can also see Venn Diagrams for the same.For simplicity, I'm using the same model here with smaller versions of the validation set to display this functionality.interp1=ClassificationInterpretationEx.from_learner(learn1)interp2=ClassificationInterpretationEx.from_learner(learn2)interp3=ClassificationInterpretationEx.from_learner(learn3)interp1.compute_label_confidence()interp2.compute_label_confidence()interp3.compute_label_confidence()%%capturefig,common_labels=compare_venn(conf_level=(0,99),interps=[interp1,interp2],mode='accurate',return_common=True,return_fig=True,set_color='tomato')fig%%capturefig,common_labels=compare_venn(conf_level=(0,99),interps=[interp1,interp2,interp3],mode='accurate',return_common=True,return_fig=True,set_color='tomato')figAugmentationApplyPILFilter, not surprisingly, lets you apply one or morePIL.ImageFilters as a data augmentation.There's also a convenience functionread_lutwhich lets you read in a LUT file (commonly found with.cubeextensions), and construct aPIL.ImageFilter.Color3dLUTto apply as a transform.The idea place for this in a fastai2 pipeline is as anitem_tfmsas it's a lossless transform and can be done right after reading the image from disk. A full example is shown in the docs.fromfastai2_extensions.augment.pil_filtersimport*lut=read_lut('../assets/luts/2strip.cube')fname='../assets/imgs/office-standoff.png'img_raw=PILImage.create(fname)img_filt=ApplyPILFilter(lut,p=1.0)(fname,split_idx=0)%%capturefig,ax=plt.subplots(nrows=1,ncols=2,figsize=(16,6))show_tensor=lambdax,ax:ToTensor()(x).show(ctx=ax)show_tensor(img_raw,ax[0])show_tensor(img_filt,ax[1])ax[0].set_title('Original')ax[1].set_title('LUT Transformed')figExportConvenience wrappers to export toONNX.Other frameworks will be added soon.ONNX#hide_outputfromfastai2_extensions.inference.exportimport*torch_to_onnx(learn.model,activation=nn.Softmax(-1),save_path=Path.home()/'Desktop',model_fname='onnx-model',input_shape=(1,3,224,224),input_name='input_image',output_names='output')Loading, polishing, and optimising exported model from /Users/rahulsomani/Desktop/onnx-model.onnx
Exported successfullypath_onnx_model='/Users/rahulsomani/Desktop/onnx-model.onnx'fname='../assets/imgs/odyssey-ape.png'fromonnxruntimeimportInferenceSessionsession=InferenceSession(path_onnx_model)x={session.get_inputs()[0].name:torch_to_numpy(preprocess_one(fname))}# preprocessing - varies based on your trainingsession.run(None,x)[array([[0.6942669 , 0.30573303]], dtype=float32)]