I want to use my trained model to predict a lot of new images. These images are placed in many different folders. Is it correct with the following data processing? My result seems to be wrong, because I followed the test steps to process the picture, I don’t know what went wrong, maybe I should not predict the new pictures like this.Please help me.Thanks.
Any suggestions would be really helpful. Thanks in advance.
Below is my code：
data_transforms = {
‘images3.6(081417-081528)’: transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
]),
}
data_dir = ‘20170522’
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
data_transforms[x])
for x in [‘images3.6(081417-081528)’]}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=2,
shuffle=False, num_workers=4)
for x in [‘images3.6(081417-081528)’]}
dataset_sizes = {x: len(image_datasets[x]) for x in [‘images3.6(081417-081528)’]}
class_names = image_datasets[‘images3.6(081417-081528)’].classes
device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)
def inference(model):
model.eval()
img= 0
with torch.no_grad():
for i, (inputs, labels) in enumerate(dataloaders[‘images3.6(081417-081528)’]):
inputs = inputs.to(device)
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
        for k in range(2):  
            img+= 1
            predclass=class_names[preds[k]]            
            _path='./classresult'
            new_path = os.path.join(_path , predclass)
            if not os.path.isdir(new_path):
                os.mkdir(new_path)
            imges=inputs.cpu().data[k]
            vutils.save_image(imges,new_path+'/'+str(img)+'.png')

model_ft = torch.load(“model.pkl”)
model_ft = model_ft.to(device)
inference(model_ft)