def create(): url = 'https://storage.googleapis.com/luizgh-datasets/avc_models/resnext50_32x4d_ddn.pt' weights_path = zoo.fetch_weights(url) state_dict = torch.load(weights_path) image_mean = torch.tensor([0.4802, 0.4481, 0.3975]).view(1, 3, 1, 1) image_std = torch.tensor([0.2770, 0.2691, 0.2821]).view(1, 3, 1, 1) m = resnext50_32x4d() model = NormalizedModel(model=m, mean=image_mean, std=image_std) model.load_state_dict(state_dict) model.eval() fmodel = foolbox.models.PyTorchModel(model, (0, 1), num_classes=200) return fmodel
m = resnet18() attacks = [ DDN(100, device=device), ] model = NormalizedModel(m, image_mean, image_std) state_dict = torch.load(args.model_path) model.load_state_dict(state_dict) model.eval().to(device) def black_box_model(img): t_img = torch.from_numpy(img).float().div(255).permute(2, 0, 1) t_img = t_img.unsqueeze(0).to(device) with torch.no_grad(): return model(t_img).argmax() smodel = resnext50_32x4d() smodel = NormalizedModel(smodel, image_mean, image_std) state_dict = torch.load(args.surrogate_model_path) smodel.load_state_dict(state_dict) smodel.eval().to(device) surrogate_models = [smodel] label = black_box_model(img) t_img = torch.from_numpy(img).float().div(255).permute(2, 0, 1).unsqueeze(0) t_img = t_img.to(device) # When the label predicted by the avatar model is not equal to that predicted by the attack model if smodel(t_img).argmax() != black_box_model(img): adv, test_num = untagetattack(model=black_box_model, s_models=surrogate_models, attacks=attacks,
def deploy(file_path=None, uploaded_image=uploaded_image, uploaded=False, demo=True): #Load the model and the weights device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = resnext50_32x4d(CFG.model_name, pretrained=False) states = [load_state(my_path + '/weights/resnext50_32x4d_fold0_best.pth')] #For Grad-cam features final_conv = model.model.layer4[2]._modules.get('conv3') fc_params = list(model.model._modules.get('fc').parameters()) #Display the uploaded/selected image st.markdown('***') st.markdown(model_predicting, unsafe_allow_html=True) if demo: test_loader = Loader(img_path=file_path) image_1 = cv2.imread(file_path) if uploaded: test_loader = Loader(uploaded_image=uploaded_image, upload_state=True, demo_state=False) image_1 = file_path st.sidebar.markdown(image_uploaded_success, unsafe_allow_html=True) st.sidebar.image(image_1, width=301, channels='BGR') for img in test_loader: activated_features = SaveFeatures(final_conv) #Save weight from fc weight = np.squeeze(fc_params[0].cpu().data.numpy()) #Inference logits, output = inference(model, states, img, device) pred_idx = output.to('cpu').numpy().argmax(1) #Grad-cam image display cur_images = img.cpu().numpy().transpose((0, 2, 3, 1)) heatmap = getCAM(activated_features.features, weight, pred_idx) plt.imshow( cv2.cvtColor((cur_images[0] * 255).astype('uint8'), cv2.COLOR_BGR2RGB)) plt.imshow(cv2.resize((heatmap * 255).astype('uint8'), (328, 328), interpolation=cv2.INTER_LINEAR), alpha=0.4, cmap='jet') plt.savefig(output_image) #Display Unknown class if the highest probability is lower than 0.5 if np.amax(logits) < 0.57: st.markdown(unknown, unsafe_allow_html=True) st.sidebar.markdown(unknown_side, unsafe_allow_html=True) st.sidebar.markdown(unknown_w, unsafe_allow_html=True) #Display the class predicted if the highest probability is higher than 0.5 else: if pred_idx[0] == 0: st.markdown(class0, unsafe_allow_html=True) st.sidebar.markdown(class0_side, unsafe_allow_html=True) st.write( " The predicted class is: **Cassava Bacterial Blight (CBB)**" ) elif pred_idx[0] == 1: st.markdown(class1, unsafe_allow_html=True) st.sidebar.markdown(class1_side, unsafe_allow_html=True) st.write( "The predicted class is: **Cassava Brown Streak Disease (CBSD)**" ) elif pred_idx[0] == 2: st.markdown(class2, unsafe_allow_html=True) st.sidebar.markdown(class2_side, unsafe_allow_html=True) st.write( "The predicted class is: **Cassava Green Mottle (CGM)**") elif pred_idx[0] == 3: st.markdown(class3, unsafe_allow_html=True) st.sidebar.markdown(class3_side, unsafe_allow_html=True) st.write( "The predicted class is: **Cassava Mosaic Disease (CMD)**") elif pred_idx[0] == 4: st.markdown(class4, unsafe_allow_html=True) st.sidebar.markdown(class4_side, unsafe_allow_html=True) st.write("The predicted class is: **Healthy**") st.sidebar.markdown( '**Scroll down to read the full report (Grad-cam and class probabilities)**' ) #Display the Grad-Cam image st.title('**Grad-cam visualization**') st.write( 'Grad-cam highlights the important regions in the image for predicting the class concept. It helps to understand if the model based its predictions on the correct regions of the image.' ) st.write( '*Grad-Cam is facing some color channels conflict. I am working on fixing the bug!*' ) gram_im = cv2.imread(output_image) st.image(gram_im, width=528, channels='RGB') #Display the class probabilities table st.title('**Class predictions:**') if np.amax(logits) < 0.57: st.markdown(unknown_msg, unsafe_allow_html=True) classes['class probability %'] = logits.reshape(-1).tolist() classes['class probability %'] = classes['class probability %'] * 100 classes_proba = classes.style.background_gradient(cmap='Reds') st.write(classes_proba) del model, states, fc_params, final_conv, test_loader, image_1, activated_features, weight, cur_images, heatmap, gram_im, logits, output, pred_idx, classes_proba gc.collect()
def load_state(model_path): model = resnext50_32x4d(CFG.model_name, pretrained=False) model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu'))['model'], strict=True) state_dict = torch.load(model_path, map_location=torch.device('cpu'))['model'] return state_dict