|
| 1 | + |
| 2 | +import random |
| 3 | +import time |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import ppafm.ml.AuxMap as aux |
| 8 | +import ppafm.ocl.field as FFcl |
| 9 | +import ppafm.ocl.oclUtils as oclu |
| 10 | +import ppafm.ocl.relax as oclr |
| 11 | +import torch |
| 12 | +from ppafm.ml.Generator import InverseAFMtrainer |
| 13 | +from ppafm.ocl.AFMulator import AFMulator |
| 14 | + |
| 15 | +import mlspm.preprocessing as pp |
| 16 | +from mlspm.datasets import download_dataset |
| 17 | +from mlspm.models import EDAFMNet |
| 18 | + |
| 19 | + |
| 20 | +class Trainer(InverseAFMtrainer): |
| 21 | + |
| 22 | + # Override this method to set the Xe tip at a different height |
| 23 | + def handle_distance(self): |
| 24 | + if self.afmulator.iZPP == 54: |
| 25 | + self.distAboveActive = self.distAboveXe |
| 26 | + super().handle_distance() |
| 27 | + if self.afmulator.iZPP == 54: |
| 28 | + self.distAboveActive = self.distAbove |
| 29 | + |
| 30 | +def apply_preprocessing(batch): |
| 31 | + Xs, Ys, _ = batch |
| 32 | + Xs = [x[...,2:8] for x in Xs] |
| 33 | + pp.add_norm(Xs) |
| 34 | + pp.add_noise(Xs, c=0.08, randomize_amplitude=False) |
| 35 | + return Xs, Ys |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + |
| 39 | + # # Independent tips model |
| 40 | + # model_type = "base" # Type of pretrained weights to use |
| 41 | + # save_file = Path("mse_independent_Xe.csv") # File to save MSE values into |
| 42 | + |
| 43 | + # Matched tips model |
| 44 | + model_type = "matched-tips" # Type of pretrained weights to use |
| 45 | + save_file = Path("./mse_matched_Xe.csv") # File to save MSE values into |
| 46 | + |
| 47 | + device = "cuda" # Device to run inference on |
| 48 | + molecules_dir = Path("../../molecules") # Path to molecule database |
| 49 | + test_heights = np.linspace(4.9, 5.7, 21) # Test heights to run |
| 50 | + n_samples = 3000 # Number of samples to run |
| 51 | + |
| 52 | + if save_file.exists(): |
| 53 | + raise RuntimeError("Save file already exists") |
| 54 | + |
| 55 | + afmulator_args = { |
| 56 | + "pixPerAngstrome" : 20, |
| 57 | + "scan_dim" : (128, 128, 19), |
| 58 | + "scan_window" : ((2.0, 2.0, 6.0), (18.0, 18.0, 7.9)), |
| 59 | + "df_steps" : 10, |
| 60 | + "tipR0" : [0.0, 0.0, 4.0] |
| 61 | + } |
| 62 | + |
| 63 | + generator_kwargs = { |
| 64 | + "batch_size" : 30, |
| 65 | + "distAbove" : 5.3, |
| 66 | + "iZPPs" : [8, 54], |
| 67 | + "Qs" : [[ -10, 20, -10, 0 ], [ 30, -60, 30, 0 ]], |
| 68 | + "QZs" : [[ 0.1, 0, -0.1, 0 ], [ 0.1, 0, -0.1, 0 ]] |
| 69 | + } |
| 70 | + |
| 71 | + # Set random seed for reproducibility |
| 72 | + random.seed(0) |
| 73 | + |
| 74 | + # Initialize OpenCL environment on GPU |
| 75 | + env = oclu.OCLEnvironment( i_platform = 0 ) |
| 76 | + FFcl.init(env) |
| 77 | + oclr.init(env) |
| 78 | + |
| 79 | + # Define AFMulator |
| 80 | + afmulator = AFMulator(**afmulator_args) |
| 81 | + afmulator.npbc = (0,0,0) |
| 82 | + |
| 83 | + # Define AuxMaps |
| 84 | + aux_maps = [ |
| 85 | + aux.ESMapConstant( |
| 86 | + scan_dim = afmulator.scan_dim[:2], |
| 87 | + scan_window = [afmulator.scan_window[0][:2], afmulator.scan_window[1][:2]], |
| 88 | + height = 4.0, |
| 89 | + vdW_cutoff = -2.0, |
| 90 | + Rpp = 1.0 |
| 91 | + ) |
| 92 | + ] |
| 93 | + |
| 94 | + # Download molecules if not already there |
| 95 | + download_dataset("ED-AFM-molecules", molecules_dir) |
| 96 | + |
| 97 | + # Define generator |
| 98 | + xyz_paths = (molecules_dir / "test").glob("*.xyz") |
| 99 | + trainer = Trainer(afmulator, aux_maps, xyz_paths, **generator_kwargs) |
| 100 | + |
| 101 | + # Pick samples |
| 102 | + random.shuffle(trainer.molecules) |
| 103 | + trainer.molecules = trainer.molecules[:n_samples] |
| 104 | + |
| 105 | + # Make model |
| 106 | + model = EDAFMNet(device=device, pretrained_weights=model_type) |
| 107 | + |
| 108 | + # Initialize save file |
| 109 | + with open(save_file, "w") as f: |
| 110 | + pass |
| 111 | + |
| 112 | + # Calculate MSE at every height for every batch |
| 113 | + start_time = time.time() |
| 114 | + total_len = len(test_heights)*len(trainer) |
| 115 | + for ih, height in enumerate(test_heights): |
| 116 | + |
| 117 | + print(f"Height = {height:.2f}") |
| 118 | + trainer.distAboveXe = height |
| 119 | + |
| 120 | + mses = [] |
| 121 | + for ib, batch in enumerate(trainer): |
| 122 | + |
| 123 | + X, ref = apply_preprocessing(batch) |
| 124 | + X = [torch.from_numpy(x).unsqueeze(1).to(device) for x in X] |
| 125 | + ref = [torch.from_numpy(r).to(device) for r in ref] |
| 126 | + |
| 127 | + with torch.no_grad(): |
| 128 | + pred, _ = model(X) |
| 129 | + pred = pred[0] |
| 130 | + |
| 131 | + diff = pred - ref[0] |
| 132 | + for d in diff: |
| 133 | + mses.append((d**2).mean().cpu().numpy()) |
| 134 | + |
| 135 | + eta = (time.time() - start_time) * (total_len / (ih*len(trainer)+ib+1) - 1) |
| 136 | + print(f"Batch {ib+1}/{len(trainer)} - ETA: {eta:.1f}s") |
| 137 | + |
| 138 | + with open(save_file, "a") as f: |
| 139 | + f.write(f"{height:.2f},") |
| 140 | + f.write(",".join([str(v) for v in mses])) |
| 141 | + f.write("\n") |
0 commit comments