-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.py
74 lines (59 loc) · 2.63 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ---------------------------------------------------
# Prevent file locking errors
# ---------------------------------------------------
import os
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
# ---------------------------------------------------
# Imports
# ---------------------------------------------------
import time
import numpy as np
import pandas as pd
import tensorflow as tf
def getFormattedTimestamp():
from datetime import datetime
# Format the timestamp
formatted_timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M')
return formatted_timestamp
def getSavePath(modelname, layerID, nExp, runID=None, mkdir=True):
from pathlib import Path
if not runID:
runID = getFormattedTimestamp()
pathName = f"results/model_{modelname}/layerID_{layerID}/nExp_{nExp}/runID_{runID}/"
if mkdir:
Path(pathName).mkdir(parents=True, exist_ok=True)
return pathName
def parseArguments():
# ---------------------------------------------------
# Parse arguments from command line
# ---------------------------------------------------
import argparse
parser = argparse.ArgumentParser(
description='Run the energy sign recovery.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# ---- add arguments to parser
parser.add_argument('--model', type=str,
help='The path to a keras.model (https://www.tensorflow.org/tutorials/keras/save_and_load).')
parser.add_argument('--layerID', type=int,
help='The ID of your target layer (as enumerated in model.layers).')
parser.add_argument('--tgtNeurons', nargs='+',
help="Specific target neuron IDs, e.g. '0 10 240'")
parser.add_argument('--runID', type=str,
help="A manual run ID (otherwise a time-tag run ID will be auto generated).")
parser.add_argument('--dataset', type=str,
help="If 'None' a random point will be chosen. If 'CIFAR10' a random CIFAR10 test image will be chosen as input.")
parser.add_argument('--eps', type=int,
help="(Optional) Precision parameter for lastLayer method (wiggle size is 10^-eps). Recommended 3 < eps < 8.")
# ---- default values
defaults = {'model': "./deti/modelweights/model_cifar10_256_256_256_256.keras",
'layerID': 3,
'tgtNeurons': None,
'nExp': 200,
'runID': None,
'dataset': None,
'eps': 8,
}
# ---- parse args
parser.set_defaults(**defaults)
args = parser.parse_args()
return args