def __init__(self, device=0, logger_name=None): super().__init__(device=device) self.model = SEResnext50_32x4d(pretrained="imagenet") self.model.to(device) self.optimizer = torch.optim.Adam(self.model.parameters(), lr=1e-4) self.Engine = Engine(self.model, self.optimizer, device) self.logger = logging.getLogger(logger_name)
def predict(image_path, model): ''' Make prediction using trained model ''' test_aug = albumentations.Compose([ albumentations.Normalize(mean=MEAN, std=STD, max_pixel_value=255.0, always_apply=True) ]) test_images = [image_path] test_targets = [0] test_dataset = ClassificationDataset(image_paths=test_images, targets=test_targets, resize=None, augmentations=test_aug) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=1, shuffle=False, num_workers=0) engine = Engine(model=model, optimizer=None, device=DEVICE) predictions = engine.predict(data_loader=test_loader) return np.vstack((predictions)).reshape(-1)
image_paths=train_images, targets=train_targets, resize=(128, 128), augmentations=aug, ).fetch(batch_size=16, num_workers=4, drop_last=False, shuffle=True, tpu=False) valid_loader = ClassificationDataLoader( image_paths=valid_images, targets=valid_targets, resize=(128, 128), augmentations=aug, ).fetch(batch_size=16, num_workers=4, drop_last=False, shuffle=False, tpu=False) optimizer = torch.optim.Adam(model.parameters(), lr=5e-4) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=15, gamma=0.6) eng = Engine(model, optimizer, device=args.device) for epoch in range(args.epochs): train_loss = eng.train(train_loader) valid_loss = eng.evaluate(valid_loader) print(f"{epoch}, Train Loss={train_loss} Valid Loss={valid_loss}") test_df = pd.read_csv(os.path.join(args.data_path, "test.csv")) images = test_df.image_id.values.tolist() images = [os.path.join(args.data_path, "images", i + ".jpg") for i in images] targets = np.zeros((len(images), 4)) aug = albumentations.Compose( [albumentations.Normalize(mean, std, max_pixel_value=255.0, always_apply=True)] )
def train(): df = pd.read_csv(DATA_PATH + "images_labeled.csv") X_train, X_test, y_train, y_test = train_test_split(df.image.values, df.label.values, test_size=0.3, random_state=42, shuffle=True) train_aug = albumentations.Compose([ albumentations.Normalize(mean=MEAN, std=STD, max_pixel_value=255.0, always_apply=True) ]) valid_aug = albumentations.Compose([ albumentations.Normalize(mean=MEAN, std=STD, max_pixel_value=255.0, always_apply=True) ]) train_images = [os.path.join(DATA_PATH, filename) for filename in X_train] valid_images = [os.path.join(DATA_PATH, filename) for filename in X_test] train_dataset = ClassificationDataset( image_paths=train_images, targets=y_train, resize=None, augmentations=train_aug, ) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=TRAIN_BS, shuffle=True, num_workers=4) valid_dataset = ClassificationDataset( image_paths=valid_images, targets=y_test, resize=None, augmentations=valid_aug, ) valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=VALID_BS, shuffle=False, num_workers=4) model = CV_Model(pretrained="imagenet") model.to(DEVICE) optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=3, mode="max") es = EarlyStopping(patience=5, mode="max") for epoch in range(EPOCHS): engine = Engine(model=model, optimizer=optimizer, device=DEVICE) engine.train(train_loader) predictions = engine.predict(valid_loader) predictions = np.vstack((predictions)).ravel() auc = metrics.roc_auc_score(y_test, predictions) print(f"Epoch = {epoch}, AUC = {auc}") scheduler.step(auc) es(auc, model, model_path=os.path.join(MODEL_PATH, "model.bin")) if es.early_stop: print("Early stopping") break
def train(fold): training_data_path = "/home/prakhar/Desktop/ml/Melanoma_Detection/input/" model_path = "/home/prakhar/Desktop/ml/Melanoma_Detection/model_weights/" df = pd.read_csv( "/home/prakhar/Desktop/ml/Melanoma_Detection/input/train_folds.csv") device = "cuda" epochs = 50 train_bs = 32 valid_bs = 16 mean = (0.485, 0.456, 0.406) std = (0.229, 0.224, 0.255) # mean = (0.485, 0.456, 0.406) # std = (0.229, 0.224, 0.225) df_train = df[df.kfold != fold].reset_index(drop=True) df_valid = df[df.kfold == fold].reset_index(drop=True) train_aug = albumentations.Compose([ albumentations.Normalize(mean, std, max_pixel_value=255.0, always_apply=True) ]) valid_aug = albumentations.Compose([ albumentations.Normalize(mean, std, max_pixel_value=255.0, always_apply=True) ]) train_images = df_train.image_name.values.tolist() train_images = [ os.path.join(training_data_path, i + '.jpg') for i in train_images ] train_targets = df_train.target.values valid_images = df_valid.image_name.values.tolist() valid_images = [ os.path.join(training_data_path, i + '.jpg') for i in valid_images ] valid_targets = df_valid.target.values train_dataset = ClassificationDataLoader(image_paths=train_images, targets=train_targets, resize=None, augmentations=train_aug) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=train_bs, shuffle=False, num_workers=4) valid_dataset = ClassificationDataLoader(image_paths=valid_images, targets=valid_targets, resize=None, augmentations=valid_aug) valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=valid_bs, shuffle=False, num_workers=4) model = SEResNext50_32x4d(pretrained='imagenet') model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, patience=3, mode="max") es = EarlyStopping(patience=5, mode="max") for epoch in range(epochs): engine = Engine(model, optimizer, device) training_loss = engine.train(train_loader) predictions, valid_loss = engine.evaluate(valid_loader, return_predictions=True) predictions = np.vstack((predictions)).ravel() auc = metrics.roc_auc_score(valid_targets, predictions) scheduler.step(auc) print(f"epoch={epoch}, auc={auc}") es(auc, model, os.path.join(model_path, f"model{fold}.bin")) if es.early_stop: print("early stopping") break