def __init__(self): backbone = 'mobilenetv3_small' self.model = LivenessNet(backbone) self.cfg = load_yaml('./configs/retinaface_mbv2.yaml') self.detection_model = tf.saved_model.load( 'saved_model/detection_model') self.model = tf.keras.models.load_model('saved_model/spoof_model')
import shutil import cv2 import numpy as np from config import IMG_HEIGHT, IMG_WIDTH from model.livenessnet import LivenessNet from model.mobilenetv3.mobilenetv3_factory import build_mobilenetv3 import matplotlib.pyplot as plt colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] backbone = 'mobilenetv3' if backbone == 'mobilenetv2': model = LivenessNet(backbone) elif backbone == 'mobilenetv3': model = build_mobilenetv3( "large", input_shape=(224, 224, 3), num_classes=2, width_multiplier=1.0, ) else: raise Exception('please choose a model!!!!!!!!!!') try: # latest_path = os.path.join('trained_models', backbone) # checkpoint = tf.train.latest_checkpoint(latest_path) # print('latest', latest) specific_checkpoint_path = os.path.join('trained_models', backbone,
import matplotlib import numpy as np from config import IMG_HEIGHT, IMG_WIDTH from model.livenessnet import LivenessNet import matplotlib.pyplot as plt import tensorflow as tf from sklearn.metrics import confusion_matrix from utils.utils import plot_confusion_matrix colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] backbone = 'mobilenetv3_small' model = LivenessNet(backbone) try: # latest = os.path.join('trained_models', backbone, 'cp-95.ckpt') latest = tf.train.latest_checkpoint( os.path.join('trained_models', backbone)) print('latest', latest) model.load_weights(latest) except: raise Exception("No weight file!") # PATH = os.path.join('dataset_wis/train_224', category) test_dir = '/media/data/manh-nx/spoofing_/data/test/live' img_preds = [] for image_path in glob.glob(test_dir + '/*'):
class TestLiveness: def __init__(self): backbone = 'mobilenetv3_small' self.model = LivenessNet(backbone) self.cfg = load_yaml('./configs/retinaface_mbv2.yaml') self.detection_model = tf.saved_model.load( 'saved_model/detection_model') self.model = tf.keras.models.load_model('saved_model/spoof_model') # try: # # latest = os.path.join('trained_models', backbone, 'cp-05.ckpt') # # latest_path = os.path.join('trained_models', backbone) # latest = tf.train.latest_checkpoint(latest_path) # print('latest', latest) # self.model.load_weights(latest) # except: # raise Exception("No weight file!") def run_(self, video_path, true_label): print(video_path, true_label) cap = cv2.VideoCapture(video_path) while cap.isOpened(): # Capture frame-by-frame try: ret, frame = cap.read() img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img = np.float32(img.copy()) img_detection, pad_params = pad_input_image( img, max_steps=max(self.cfg['steps'])) # run model outputs = self.detection_model(img_detection[np.newaxis, ...]).numpy() # recover padding effect outputs = recover_pad_output(outputs, pad_params) # print(len(outputs)) if len(outputs) != 1: # print('no face') continue img_pred = cv2.resize(img, (IMG_HEIGHT, IMG_WIDTH)) # cv2.imwrite(f'x1/{round(time.time() * 1000)}.jpg', cv2.cvtColor(img_pred, cv2.COLOR_RGB2BGR)) img_pred = (img_pred / 255.0) img_pred = np.expand_dims(img_pred, axis=0) net_out_value = self.model.predict(img_pred) if net_out_value[0][0] > 0.5: predicted_label = 'spoof' else: predicted_label = 'live' print(predicted_label) if true_label != predicted_label: print(net_out_value, 'wrong prediction') cv2.imwrite( f'{destination_data_folder}/{true_label}/{round(time.time() * 1000)}.jpg', frame) cv2.imshow('Windows', frame) if cv2.waitKey(1) == 27: exit(0) except Exception as e: print(e) break
# construct the training image generator for data augmentation aug = ImageDataGenerator( rotation_range=20, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest", ) # initialize the optimizer and model print("[INFO] compiling model...") opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS) model = LivenessNet.build(width=32, height=32, depth=3, classes=len(le.classes_)) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) # train the network print("[INFO] training network for {} epochs...".format(EPOCHS)) H = model.fit_generator( aug.flow(trainX, trainY, batch_size=BS), validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS, epochs=EPOCHS, ) # evaluate the network print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=BS)
directory=validation_dir, target_size=(IMG_HEIGHT, IMG_WIDTH), class_mode='binary', ) # image_batch, label_batch = next(train_generator) # # class_names = list(train_generator.class_indices.keys()) # print('label', label_batch, class_names) # # show_batch_binary(image_batch, label_batch, class_names) ####################################################################################################################### backbone = 'mobilenetv3_small' model = LivenessNet(backbone) # # initial_weights = os.path.join(tempfile.mkdtemp(), 'initial_weights') # model.save_weights(initial_weights) # initialize the optimizer and compile the model model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(), metrics=METRICS) model.build((None, 224, 224, 3)) model.summary() for layer in model.layers: print(layer.trainable)
self.model.compile(optimizer=self.optimizer, loss="sparse_categorical_crossentropy", metrics=["accuracy"]) print(self.model.summary()) early_stopping = tf.keras.callbacks.EarlyStopping(monitor="val_loss", mode="min", verbose=1, patience=10) history = self.model.fit(self.train_generator, epochs=self.epochs, validation_data=self.validation_generator, verbose=1, steps_per_epoch=self.step_size_train, validation_steps=self.step_size_validation, callbacks=[early_stopping]) #, steps_per_epoch=len(self.train_generator) / self.epochs self.model.save(self.config["trial_saved_model"]) return history if __name__ == "__main__": dl = DataLoader("../config.json") model = LivenessNet("../config.json").build() trainer = Trainer(dl, model, config="../config.json") history = trainer.train()
import os import cv2 import numpy as np import tensorflow as tf from model.livenessnet import LivenessNet print(tf.version.VERSION) backbone = 'mobilenetv3_small' model = LivenessNet(backbone) try: # latest = os.path.join('trained_models', backbone, 'cp-05.ckpt') # latest_path = os.path.join('trained_models', backbone) latest = tf.train.latest_checkpoint(latest_path) print('latest', latest) model.load_weights(latest) except: raise Exception("No weight file!") # image_path = 'data/test_224/spoof/download.png' # img_preds = [] # img = cv2.imread(image_path, cv2.COLOR_BGR2RGB) # # img = cv2.resize(img, (IMG_HEIGHT, IMG_WIDTH)) # # img_pred = (img / 255.0) # # img_preds.append(img_pred) # img_preds = np.array(img_preds)