def predict(fold=0): df = pd.read_csv("../input/plant-pathology-2020-fgvc7/test.csv") device = "cuda" if torch.cuda.is_available() else "cpu" imgs = df.image_id.values.tolist() path = "../input/plant-images-224-224/" test_imgs = [path + file + ".png" for file in imgs] test_aug = Utils.get_aug("test") test_tar = np.zeros((len(imgs), 4)) test_dataset = ClassificationLoader(image_paths=test_imgs, targets=test_tar, resize=None, augmentations=test_aug) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=bs_test, num_workers=0, shuffle=False) model = get_model(model_name) model_save_path = f"./model_fold_{fold}.bin" model.load_state_dict(torch.load(model_save_path)) model = model.to(device) engine = Engine(model, None, device) preds = engine.predict(test_loader) preds = np.vstack(preds) return preds
def predict(fold,model_name): df=pd.read_csv("E:/kaggle_imgs/Plant-pathology-2020/Data/train_fold.csv")[0:10] device="cuda" if torch.cuda.is_available() else "cpu" imgs=df.image_id.values.tolist() path="E:/kaggle_imgs/Plant-pathology-2020/images_224_224/" test_imgs=[path+file+".png" for file in imgs] utils=Utils(mode="test") test_aug=utils.get_aug() test_tar=np.zeros((len(imgs),4)) test_dataset=ClassificationLoader( image_paths=test_imgs,targets=test_tar,resize=None,augmentations=test_aug ) test_loader=torch.utils.data.DataLoader( test_dataset,batch_size=bs_test,num_workers=4,shuffle=False ) model=get_effinet(classes=2) model_save_path=f"./model_fold_{fold}.bin" model.load_state_dict(torch.load(model_save_path)) model=model.to(device) engine=Engine(model,None,device,classes=2,weights=None) preds=engine.predict(test_loader) preds=np.vstack(preds) #script to c++ sample=torch.rand(1,3,224,224) model.to("cpu") traced_script_module = torch.jit.trace(model,sample) traced_script_module.save("E:/temp/saved_models/"+f"/traced_1015_fold_{fold}.pt") return preds
def predict(fold): df = train[0:10] device = "cuda" if torch.cuda.is_available() else "cpu" test_imgs = df.tar_path.values.tolist() test_aug = Utils.get_aug("test") test_tar = np.zeros((len(test_imgs), 2)) test_dataset = ClassificationLoader(image_paths=test_imgs, targets=test_tar, resize=None, augmentations=test_aug) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=bs_valid, num_workers=0, shuffle=False) model = get_model_effi_b4(2) tm = datetime.datetime.now().strftime("%m%d") model_save_path = f"E:/kaggle_imgs/H2/saved_models/model_fold_{fold}_{tm}.bin" model.load_state_dict(torch.load(model_save_path)) model = model.to(device) engine = Engine(model, None, device, classes=2, weights=None) preds = engine.predict(test_loader) preds = np.vstack(preds) #.argmax(axis=1) #script to c++ sample = torch.rand(1, 3, 224, 224) model.to("cpu") model.set_swish(False) traced_script_module = torch.jit.trace(model, sample) traced_script_module.save( f"E:/kaggle_imgs/H2/saved_models/traced_{model_name}_fold_{fold}_{tm}.pt" ) return preds