Giter Club home page Giter Club logo

facenet-face-recognition's Introduction

facenet-face-recognition

This repository contains a demonstration of face recognition using the FaceNet network (https://arxiv.org/pdf/1503.03832.pdf) and a webcam. Our implementation feeds frames from the webcam to the network to determine whether or not the frame contains an individual we recognize.

How to use

To install all the requirements for the project run

pip install -r requirements.txt

In the root directory. After the modules have been installed you can run the project by using python

python facenet.py

NOTE

We are using the Windows 10 Text-to-Speech library to output our audio message, so if you want to run it on a different OS then you will have to replace the speech library used in facenet.py

facenet-face-recognition's People

Contributors

skuldur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

facenet-face-recognition's Issues

Error running the code after cloning and installing requirements

After cloning and installing requirements, I tried to run the system but it keeps on returning me this error. Anyone that can help me?

File "facenet.py", line 194, in
database = prepare_database()
File "facenet.py", line 57, in prepare_database
database[identity] = img_path_to_encoding(file, FRmodel)
File "D:\NOT FOR EVERYONE\DO NOT OPEN\EJ -Projects\VUI\Recognition\facenet-face-recognition\fr_utils.py", line 194, in img_path_to_encoding
return img_to_encoding(img1, model)
File "D:\NOT FOR EVERYONE\DO NOT OPEN\EJ -Projects\VUI\Recognition\facenet-face-recognition\fr_utils.py", line 202, in img_to_encoding
embedding = model.predict_on_batch(x_train)
File "C:\Anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1820, in predict_on_batch
outputs = self.predict_function(iterator)
File "C:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in call
result = self._call(*args, **kwds)
File "C:\Anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 894, in _call
return self._concrete_stateful_fn._call_flat(
File "C:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py", line 1918, in _call_flat
return self._build_call_outputs(self._inference_function.call(
File "C:\Anaconda\lib\site-packages\tensorflow\python\eager\function.py", line 555, in call
outputs = execute.execute(
File "C:\Anaconda\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Default MaxPoolingOp only supports NHWC on device type CPU
[[node FaceRecoModel/max_pooling2d/MaxPool (defined at D:\NOT FOR EVERYONE\DO NOT OPEN\EJ -Projects\VUI\Recognition\facenet-face-recognition\fr_utils.py:202) ]] [Op:__inference_predict_function_3592]

Function call stack:
predict_function

Ambiguous recognition by the pre trained model

Can you share your training scripts. the current model doesn't seem to successfully recognize and assign correct labels to people. I tried playing with hyper parameter(set to 0.52 in facenet.py) but for more than one labels in gallery it seems to fail.

Could not install pypiwin32

Hi,

While I was installing the modules that is needed in requirements.txt. I could install pypiwin32, I tried sudo pip3 install pypiwin32 but it shows this error:

Collecting pywin32
Could not find a version that satisfies the requirement pywin32 (from versions: )
No matching distribution found for pywin32

Does anyone know how to solve this?

camera won't open

When I run the code it doesn't prompt with any error. But instead it keeps loading for a while then a frame opens for less than a second and closes again.

The voice message is not working

Hi i tried running the code but unable to listen to anything only can see printed messages on my screen with the dsitance metrics values

Not able to run on Windows system

Hi,
I cloned your code and tried running on my system.
The system throws the following error:

C:>python D:\facenet-face-recognition-master\facenet.py
Using TensorFlow backend.
OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\ci\opencv_1512688052760\work\modules\objdetect\src\cascadedetect.cpp, line 1698
Traceback (most recent call last):
File "D:\facenet-face-recognition-master\facenet.py", line 199, in
webcam_face_recognizer(database)
File "D:\facenet-face-recognition-master\facenet.py", line 86, in webcam_face_recognizer
img = process_frame(img, frame, face_cascade)
File "D:\facenet-face-recognition-master\facenet.py", line 101, in process_frame
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: C:\ci\opencv_1512688052760\work\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

Please help!

I tried searching on stackOverflow. Here is what i found!!

https://stackoverflow.com/questions/30508922/error-215-empty-in-function-detectmultiscale?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

But this doesnt seem to help out!

Have to clear tensorflow graphs.

ValueError: Fetch argument <tf.Variable 'conv1/kernel:0' shape=(7, 7, 3, 64) dtype=float32_ref> cannot be interpreted as a Tensor.
(Tensor Tensor("conv1/kernel:0", shape=(7, 7, 3, 64), dtype=float32_ref) is not an element of this graph.)

I was using this model for face similarity project. But I did encounter above "ValueError".
I overcame by clearing the session.
My code is:

def faceRecoModel(input_shape):
    # Clear previous session
    K.clear_session()

    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)

    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)
    .
    # First Block
    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(X)
    X = BatchNormalization(axis=1, name='bn1')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D((3, 3), strides=2)(X)

    # Second Block
    X = Conv2D(64, (1, 1), strides=(1, 1), name='conv2')(X)
    X = BatchNormalization(axis=1, epsilon=0.00001, name='bn2')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)

    # Third Block
    X = Conv2D(192, (3, 3), strides=(1, 1), name='conv3')(X)
    X = BatchNormalization(axis=1, epsilon=0.00001, name='bn3')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D(pool_size=3, strides=2)(X)

    # Inception 1: a/b/c
    X = inception_block_1a(X)
    X = inception_block_1b(X)
    X = inception_block_1c(X)

    # Inception 2: a/b
    X = inception_block_2a(X)
    X = inception_block_2b(X)

    # Inception 3: a/b
    X = inception_block_3a(X)
    X = inception_block_3b(X)

    # Top layer
    X = AveragePooling2D(pool_size=(3, 3), strides=(1, 1), data_format='channels_first')(X)
    X = Flatten()(X)
    X = Dense(128, name='dense_layer')(X)

    # L2 normalization
    X = Lambda(lambda x: K.l2_normalize(x, axis=1))(X)

    # Create model instance
    model = Model(inputs=X_input, outputs=X, name='FaceRecoModel')

    return model

Shape must be rank 1 but is rank 4 for 'bn1/cond/FusedBatchNorm'

Running windows 10 with everything installed and I get this error:

ValueError: Shape must be rank 1 but is rank 4 for 'bn1/cond/FusedBatchNorm' (op: 'FusedBatchNorm') with input shapes: [?,64,48,48], [1,64,1,1], [1,64,1,1], [1,64,1,1], [1,64,1,1].

Any thoughts?

Full error:
Using TensorFlow backend.
2018-06-29 12:25:03.141799: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-06-29 12:25:03.498122: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1356] Found device 0 with properties:
name: GeForce GTX TITAN Black major: 3 minor: 5 memoryClockRate(GHz): 0.98
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 5.00GiB
2018-06-29 12:25:03.505607: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1435] Adding visible gpu devices: 0
2018-06-29 12:25:03.782568: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-06-29 12:25:03.787601: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:929] 0
2018-06-29 12:25:03.790697: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:942] 0: N
2018-06-29 12:25:03.795210: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4790 MB memory) -> physical GPU (device: 0, name: GeForce GTX TITAN Black, pci bus id: 0000:01:00.0, compute capability: 3.5)
Traceback (most recent call last):
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1567, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 4 for 'bn1/cond/FusedBatchNorm' (op: 'FusedBatchNorm') with input shapes: [?,64,48,48], [1,64,1,1], [1,64,1,1], [1,64,1,1], [1,64,1,1].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "facenet.py", line 19, in
FRmodel = faceRecoModel(input_shape=(3, 96, 96))
File "C:\Users\vince\Desktop\facenet-face-recognition\inception_blocks_v2.py", line 233, in faceRecoModel
X = BatchNormalization(axis = 1, name = 'bn1')(X)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\base_layer.py", line 460, in call
output = self.call(inputs, **kwargs)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\layers\normalization.py", line 204, in call
training=training)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 3069, in in_train_phase
x = switch(training, x, alt)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 3004, in switch
else_expression_fn)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\util\deprecation.py", line 432, in new_func
return func(*args, **kwargs)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2072, in cond
orig_res_f, res_f = context_f.BuildCondBranch(false_fn)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 1913, in BuildCondBranch
original_result = fn()
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\layers\normalization.py", line 165, in normalize_inference
epsilon=self.epsilon)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 1894, in batch_normalization
is_training=False
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\nn_impl.py", line 904, in fused_batch_norm
name=name)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line 3774, in _fused_batch_norm
is_training=is_training, name=name)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3392, in create_op
op_def=op_def)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1734, in init
control_input_ops)
File "C:\Users\vince\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1570, in _create_c_op
raise ValueError(str(e))
ValueError: Shape must be rank 1 but is rank 4 for 'bn1/cond/FusedBatchNorm' (op: 'FusedBatchNorm') with input shapes: [?,64,48,48], [1,64,1,1], [1,64,1,1], [1,64,1,1], [1,64,1,1].

Variable batch_normalization_1/moving_mean/biased already exists, disallowed error

Hi

I am running facenet on following configuration in Anaconda when I encounter a variable disallowed error.

Anaconda: 1.6.12
numpy: 1.12.1
Tensorflow: 1.10
Keras: 2.1.6

Have you seen this issue? Which version did you run in when you last executed the test run?

The error occurred when running block 20:

im_in = Input(shape=(200,200,4))
#wrong = Input(shape=(200,200,3))

x1 = modelsqueeze(im_in)
#x = Convolution2D(64, (5, 5), padding='valid', strides =(2,2))(x)

#x1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(x1)

"""
x1 = Convolution2D(256, (3,3), padding='valid', activation="relu")(x1)
x1 = Dropout(0.4)(x1)

x1 = MaxPooling2D(pool_size=(3, 3), strides=(1, 1))(x1)

x1 = Convolution2D(256, (3,3), padding='valid', activation="relu")(x1)
x1 = BatchNormalization()(x1)
x1 = Dropout(0.4)(x1)

x1 = Convolution2D(64, (1,1), padding='same', activation="relu")(x1)
x1 = BatchNormalization()(x1)
x1 = Dropout(0.4)(x1)
"""



x1 = Flatten()(x1)

x1 = Dense(512, activation="relu")(x1)
x1 = Dropout(0.2)(x1)
#x1 = BatchNormalization()(x1)
feat_x = Dense(128, activation="linear")(x1)
feat_x = Lambda(lambda  x: K.l2_normalize(x,axis=1))(feat_x)


model_top = Model(inputs = [im_in], outputs = feat_x)

model_top.summary()

im_in1 = Input(shape=(200,200,4))
im_in2 = Input(shape=(200,200,4))

feat_x1 = model_top(im_in1)
feat_x2 = model_top(im_in2)


lambda_merge = Lambda(euclidean_distance)([feat_x1, feat_x2])


model_final = Model(inputs = [im_in1, im_in2], outputs = lambda_merge)

model_final.summary()

adam = Adam(lr=0.001)

sgd = SGD(lr=0.001, momentum=0.9)

model_final.compile(optimizer=adam, loss=contrastive_loss)

The error is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-d87010ad9d0a> in <module>()
      2 #wrong = Input(shape=(200,200,3))
      3 
----> 4 x1 = modelsqueeze(im_in)
      5 #x = Convolution2D(64, (5, 5), padding='valid', strides =(2,2))(x)
      6 

~/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
    617 
    618             # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 619             output = self.call(inputs, **kwargs)
    620             output_mask = self.compute_mask(inputs, previous_mask)
    621 

~/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py in call(self, inputs, mask)
   2083             return self._output_tensor_cache[cache_key]
   2084         else:
-> 2085             output_tensors, _, _ = self.run_internal_graph(inputs, masks)
   2086             return output_tensors
   2087 

~/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py in run_internal_graph(self, inputs, masks)
   2233                                 if 'mask' not in kwargs:
   2234                                     kwargs['mask'] = computed_mask
-> 2235                             output_tensors = _to_list(layer.call(computed_tensor, **kwargs))
   2236                             output_masks = layer.compute_mask(computed_tensor,
   2237                                                               computed_mask)

~/anaconda3/lib/python3.6/site-packages/keras/layers/normalization.py in call(self, inputs, training)
    191         self.add_update([K.moving_average_update(self.moving_mean,
    192                                                  mean,
--> 193                                                  self.momentum),
    194                          K.moving_average_update(self.moving_variance,
    195                                                  variance,

~/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in moving_average_update(x, value, momentum)
   1003     """
   1004     return moving_averages.assign_moving_average(
-> 1005         x, value, momentum, zero_debias=True)
   1006 
   1007 

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/moving_averages.py in assign_moving_average(variable, value, decay, zero_debias, name)
     68         decay = math_ops.cast(decay, variable.dtype.base_dtype)
     69       if zero_debias:
---> 70         update_delta = _zero_debias(variable, value, decay)
     71       else:
     72         update_delta = (variable - value) * decay

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/moving_averages.py in _zero_debias(unbiased_var, value, decay)
    178         local_step_initializer = init_ops.zeros_initializer()
    179       biased_var = variable_scope.get_variable(
--> 180           "biased", initializer=biased_initializer, trainable=False)
    181       local_step = variable_scope.get_variable(
    182           "local_step",

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
   1047       collections=collections, caching_device=caching_device,
   1048       partitioner=partitioner, validate_shape=validate_shape,
-> 1049       use_resource=use_resource, custom_getter=custom_getter)
   1050 get_variable_or_local_docstring = (
   1051     """%s

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
    946           collections=collections, caching_device=caching_device,
    947           partitioner=partitioner, validate_shape=validate_shape,
--> 948           use_resource=use_resource, custom_getter=custom_getter)
    949 
    950   def _get_partitioned_variable(self,

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource, custom_getter)
    354           reuse=reuse, trainable=trainable, collections=collections,
    355           caching_device=caching_device, partitioner=partitioner,
--> 356           validate_shape=validate_shape, use_resource=use_resource)
    357 
    358   def _get_partitioned_variable(

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, use_resource)
    339           trainable=trainable, collections=collections,
    340           caching_device=caching_device, validate_shape=validate_shape,
--> 341           use_resource=use_resource)
    342 
    343     if custom_getter is not None:

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape, use_resource)
    651                          " Did you mean to set reuse=True in VarScope? "
    652                          "Originally defined at:\n\n%s" % (
--> 653                              name, "".join(traceback.format_list(tb))))
    654       found_var = self._vars[name]
    655       if not shape.is_compatible_with(found_var.get_shape()):

ValueError: Variable batch_normalization_1/moving_mean/biased already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  File "/Users/xyz/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 1005, in moving_average_update
    x, value, momentum, zero_debias=True)
  File "/Users/xyz/anaconda3/lib/python3.6/site-packages/keras/layers/normalization.py", line 193, in call
    self.momentum),
  File "/Users/xyz/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 619, in __call__
    output = self.call(inputs, **kwargs)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.