# Written by Dr Daniel Buscombe, Marda Science LLC # for the SandSnap Program # # MIT License # # Copyright (c) 2020-2021, Marda Science LLC # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. ##> Release v1.4 (Aug 2021) ###=================================================== # import libraries import gc, os, sys, shutil ###=================================================== # import and set global variables from defaults.py from defaults import * global IM_HEIGHT, IM_WIDTH global NUM_EPOCHS, SHALLOW global VALID_BATCH_SIZE, BATCH_SIZE VALID_BATCH_SIZE = BATCH_SIZE global MAX_LR, OPT, USE_GPU, DO_AUG, DO_STANDARDIZE # global STOP_PATIENCE, FACTOR, MIN_DELTA, MIN_LR # global MIN_DELTA, FACTOR, STOP_PATIENCE ##==================================================== # import tensorflow.compat.v1 as tf1 # config = tf1.ConfigProto() # config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU # config.log_device_placement = True # to log device placement (on which device the operation ran) # sess = tf1.Session(config=config) # tf1.keras.backend.set_session(sess) # PREDICT = False # # ##OS # if PREDICT == True: # os.environ['CUDA_VISIBLE_DEVICES'] = '-1' ##TF/keras if USE_GPU == True: ##use the first available GPU os.environ['CUDA_VISIBLE_DEVICES'] = '0' else: ## to use the CPU (not recommended): os.environ['CUDA_VISIBLE_DEVICES'] = '-1' import numpy as np import tensorflow as tf # from tensorflow.keras import mixed_precision # mixed_precision.set_global_policy('mixed_float16') SEED=42 np.random.seed(SEED) AUTO = tf.data.experimental.AUTOTUNE # used in tf.data.Dataset API tf.random.set_seed(SEED) print("Version: ", tf.__version__) print("Eager mode: ", tf.executing_eagerly()) print('GPU name: ', tf.config.experimental.list_physical_devices('GPU')) print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU'))) from tensorflow.keras.layers import Input, Dense, MaxPool2D, GlobalMaxPool2D from tensorflow.keras.layers import Dropout, MaxPooling2D, GlobalAveragePooling2D from tensorflow.keras.models import Model, Sequential from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, LearningRateScheduler from tensorflow.keras.layers import DepthwiseConv2D, Conv2D, SeparableConv2D from tensorflow.keras.layers import BatchNormalization, Activation, concatenate try: from tensorflow.keras.utils import plot_model except: pass import tensorflow.keras.backend as K from tensorflow.keras.utils import to_categorical import tensorflow_addons as tfa ##SKLEARN from sklearn.preprocessing import RobustScaler #MinMaxScaler from sklearn.metrics import confusion_matrix, classification_report ##OTHER from PIL import Image from glob import glob import matplotlib.pyplot as plt import pandas as pd import itertools import joblib import random from tempfile import TemporaryFile import tensorflow_addons as tfa import tqdm from skimage.transform import AffineTransform, warp #rotate,