Skip to content

Instantly share code, notes, and snippets.

@ashhadulislam
Last active February 12, 2022 11:57
Show Gist options
  • Save ashhadulislam/57418e7f88377745b3d93b2dbfec3264 to your computer and use it in GitHub Desktop.
Save ashhadulislam/57418e7f88377745b3d93b2dbfec3264 to your computer and use it in GitHub Desktop.
# read an image
from PIL import Image
from torch.autograd import Variable
loader = transforms.Compose([transforms.Resize(dim[0]),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
def image_loader(image_name):
"""load image, returns cuda tensor"""
image = Image.open(image_name)
image = loader(image).float()
image = Variable(image, requires_grad=True)
image = image.unsqueeze(0) #this is for VGG, may not be needed for ResNet
if torch.cuda.is_available():
return image.cuda() #assumes that you're using GPU
else:
return image
file_location="data/arranged/test/0/lower-puna-volcano_00000266_pre_disaster.png"
image = image_loader(file_location)
res=model_ft(image)
_, pred = torch.max(res, 1)
print("Prediction is ",pred)
file_location="data/arranged/test/1/nepal-flooding_00000332_post_disaster.png"
image = image_loader(file_location)
res=model_ft(image)
_, pred = torch.max(res, 1)
print("Prediction is ",pred)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment