I post a image with size 43kb and i am using torch in flask. i used transform to process image. my code:
### function process image
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
# TODO: Process a PIL image for use in a PyTorch model
# tensor.numpy().transpose(1, 2, 0)
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
image = preprocess(image)
return image
### predict product recommendation
def predict2(uploaded_file_path, model, topk=5):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
# TODO: Implement the code to predict the class from an image file
img = Image.open(uploaded_file_path)
img.convert("RGB")
img = process_image(img)
# Convert 2D image to 1D vector
img = np.expand_dims(img, 0)
img = torch.from_numpy(img)
model.eval()
inputs = Variable(img).to(device)
logits = model.forward(inputs)
ps = F.softmax(logits,dim=1)
topk = ps.cpu().topk(topk)
return (e.data.numpy().squeeze().tolist() for e in topk)
edit by admin [formatting]