<jupyter_start><jupyter_text>Notebook to check the working of the trained model<jupyter_code>import pickle
import torch
import torchvision
!dir
model = pickle.load(open(r"saved_model/resnetmain.pkl","rb"))#map_location=torch.device('cpu'))
checkpoint = torch.load(r"saved_model/main_model.pt",map_location=torch.device('cpu'))
model.load_state_dict(checkpoint['state_dict'])
class_names = ["safe","unsafe"]
from PIL import Image
import torchvision.transforms as transforms

standard_normalization = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                              std=[0.229, 0.224, 0.225])

def load_input_image(img_path):    
    image = Image.open(img_path).convert('RGB')
    prediction_transform = transforms.Compose([transforms.Resize(size=(224, 224)),
                                     transforms.ToTensor(), 
                                     standard_normalization])

    # discard the transparent, alpha channel (that's the :3) and add the batch dimension
    image = prediction_transform(image)[:3,:,:].unsqueeze(0)
    return image
def predict_image(model, class_names, img_path):
    # load the image and return the predicted breed
    img = load_input_image(img_path)
    model = model.cpu()
    model.eval()
    idx = torch.argmax(model(img))
    return class_names[idx]
import matplotlib.pyplot as plt                        
%matplotlib inline  

def run_app(img_path):
    
    img = Image.open(img_path)
    plt.imshow(img)
    plt.show()
    
    prediction = predict_image(model, class_names, img_path)
        
    return prediction
run_app("./test_images/test1.jpg")
run_app("./test_images/test2.jpg")
run_app("./test_images/test3.jpg")
run_app("./test_images/test4.jpg")
run_app("./test_images/test5.jpg")
run_app("./test_images/test6.jpg")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12295081967213115, 'fraction_numerical': 0.021311475409836064, 'mean_word_length': 3.422705314009662, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '314358', 'license': 'MIT', 'n_tokens_mistral': 607, 'n_tokens_neox': 580, 'n_words': 108, 'path': 'Violence-Detection/Pytorch-Implementation/try.ipynb', 'repo_name': 'dheelaiund/Jatayu-ContentModeration', 'size': '1830'}