SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import keras
  2. from keras_resnet.models import ResNet50
  3. from tensorflow.contrib.learn.python.learn.estimators._sklearn import accuracy_score
  4. import face_recognition
  5. print(keras.__version__)
  6.  
  7. import os
  8. import cv2 as cv
  9. import numpy as np
  10.  
  11. shape = (200, 200)
  12.  
  13. def get_face(img, name):
  14.     t = face_recognition.face_locations(img, model='cnn')
  15.     (top, right, bottom, left) = t[0]
  16.     face = img[top:bottom, left:right]
  17.     cv.imwrite("faces/"+name+"face.jpg",face)
  18.     return face
  19.  
  20. def get_test_data(path_to_test_data):
  21.     classes = os.listdir(path_to_test_data)
  22.     x_train = []
  23.     y_train = []
  24.     for c in classes:
  25.         imgs = os.listdir(os.path.join(path_to_test_data, c))
  26.         for img_name in imgs:
  27.             img = cv.imread(os.path.join(path_to_test_data, c, img_name))
  28.             face = get_face(img, c+"_"+img_name)
  29.             face = cv.resize(face, shape)
  30.             x_train.append(face)
  31.             y_train.append(int(c))
  32.     y_train = keras.utils.to_categorical(y_train, num_classes=2)
  33.     return np.asarray(x_train), y_train
  34.  
  35.  
  36. def get_train_data(path_to_train_data):
  37.     meta = open("data/meta.txt", "r")
  38.     x_train = []
  39.     y_train = []
  40.     for v in meta.readlines():
  41.         img_name, c = v.split(" ")
  42.         img = cv.imread(os.path.join(path_to_train_data, img_name))
  43.         img = cv.resize(img, shape)
  44.         x_train.append(img)
  45.         y_train.append(int(c))
  46.     y_train = keras.utils.to_categorical(y_train, num_classes=2)
  47.  
  48.     return np.asarray(x_train), y_train
  49.  
  50.  
  51. def get_model(shape, classes):
  52.     x = keras.layers.Input(shape)
  53.  
  54.     model = ResNet50(x, classes=classes)
  55.     model.compile(optimizer='adadelta',
  56.                   loss='binary_crossentropy',
  57.                   metrics=['accuracy'])
  58.     # ResnetBuilder.build_resnet_50((shape[2], shape[1], shape[0]), classes)
  59.     return model
  60.  
  61.  
  62. def get_pretrained_model():
  63.     from keras_retinanet import models
  64.     model = models.load_model('faceglasses.h5', backbone_name="resnet50")
  65.     return model
  66.     # f = open("model_json.json", "r")
  67.     # json = f.readline()
  68.     # from keras.models import model_from_json
  69.     # model = model_from_json(json, custom_objects={"Input":keras.layers.Input})
  70.     # model.load_weights("glasses_w.h5")
  71.     # return model
  72.  
  73.  
  74. def load_data():
  75.     return np.load("x_train.npy"), np.load("y_train.npy")
  76.  
  77. # 2.0.8
  78. # x_train, y_train = get_train_data("/home/sergej2/MeGlass_120x120")
  79. # np.save("x_train",x_train)
  80. # np.save("y_train",y_train)
  81.  
  82.  
  83. def fit_save_model():
  84.     x_train, y_train = load_data()
  85.     model = get_model(x_train[0].shape, 2)
  86.     model.fit(x_train, y_train, epochs=1, batch_size=128, validation_split=0.2, shuffle=True)
  87.     json = model.to_json()
  88.     model_f = open("model_json.json", "w")
  89.     model_f.write(json)
  90.     model_f.close()
  91.     model.save_weights("glasses_w.h5")
  92.  
  93.  
  94. x_test, y_test = get_test_data("data/test")
  95. model = get_pretrained_model()
  96. #
  97. # fit_save_model()
  98.  
  99. res = model.predict(x_test)
  100. print("Accuracy")
  101. print(accuracy_score(res, y_test))
  102.  
  103. # print(f1_measure(res, y_test))
  104. # print(model.evaluate(x_test, y_test))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top