def predict_ethnic(self, name): means = np.load('means_ethnic.npy') model = create_face_network(nb_class=4, hidden_dim=512, shape=(224, 224, 3)) model.load_weights('weights_ethnic.hdf5') im = cv2.imread(name, cv2.IMREAD_COLOR) im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) im = cv2.cvtColor(im, cv2.COLOR_GRAY2RGB) im = cv2.resize(im, (224, 224)) im = np.float64(im) im /= 255.0 im = im - means #return model.predict(np.array([im])) return model.predict(np.array([im]))
def predict_region(im): means = np.load('means_region.npy') model = create_face_network(nb_class=4, hidden_dim=256, shape=(224, 224, 3)) model.load_weights('weights_region.hdf5') im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) im = cv2.cvtColor(im, cv2.COLOR_GRAY2RGB) im = cv2.resize(im, (224, 224)) # cv2.imshow("fsfs",im) # cv2.waitKey(0) im = np.float64(im) im /= 255.0 im = im - means return model.predict(np.array([im]))
def predict_ethnic(image_path): means = np.load('means_ethnic.npy') model = create_face_network(nb_class=4, hidden_dim=512, shape=(224, 224, 3)) model.load_weights('weights_ethnic.hdf5') im = cv2.imread(image_path, cv2.IMREAD_COLOR) im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) im = cv2.cvtColor(im, cv2.COLOR_GRAY2RGB) im = cv2.resize(im, (224, 224)) im = np.float64(im) im /= 255.0 im = im - means #print("NP: ", np) #print("np.array([im]): ", np.array([im])) #print("RESULT: ", model.predict(np.array([im]))) result = model.predict(np.array([im])) race = ETHNIC[np.argmax(result)] return race
X_data = X_data[p] y_data = y_data[p] X_train = X_data[0:int(round(train_split*num_images))] y_train = y_data[0:int(round(train_split*num_images))] X_test = X_data[int(round(train_split*num_images))+1:-1] y_test = y_data[int(round(train_split*num_images))+1:-1] means = np.mean(X_train, axis = 0) X_train -= means X_test -= means np.save('means_ethnic.npy',means) opt = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True) checkpoint = ModelCheckpoint('weights_ethnic.hdf5', monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=True, mode='max') model = create_face_network(nb_class=4, hidden_dim=512, shape=(224, 224, 3)) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) model.fit(X_train, y_train, batch_size=32, epochs=10, verbose=1, callbacks=[checkpoint], validation_data=(X_test, y_test), shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0)