FASNet
FASNet copied to clipboard
Can the weight file be used directly
@OeslleLucena Hi, will the REPLAY-ftweights18.h5 and 3DMAD-ftweights18.h5 these two weights is already trained weight file? Can be used directly, or need to combine with VGG-16 weights and then training? There are two documents, is tf, or th training out?
@okmmsky888 Yep, they are already trained and they can be used directly. No need to combine with VGG-16 weights. The two documents is because they were fine-tuned for both datasets REPLAY-ATTACK and 3DMAD, separately. It was all done in theano.
@OeslleLucena hi ,I loaded REPLAY-ftweights18.h5, reported the following error, do you know where is the problem?The following is the error message reported
ValueError: ('shapes (1,25088) and (4608,256) not aligned: 25088 (dim 1) != 4608 (dim 0)', (1, 25088), (4608, 256)) Apply node that caused the error: Dot22(Reshape{2}.0, dense_1/kernel) Toposort index: 109 Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)] Inputs shapes: [(1, 25088), (4608, 256)] Inputs strides: [(100352, 4), (1024, 4)] Inputs values: ['not shown', 'not shown'] Outputs clients: [[Elemwise{Composite{((i0 + i1) + Abs((i0 + i1)))}}[(0, 0)](Dot22.0, InplaceDimShuffle{x,0}.0)]]
HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'. HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
@okmmsky888 It's a mismatch of shapes in the network.
ValueError: ('shapes (1,25088) and (4608,256) not aligned: 25088 (dim 1) != 4608 (dim 0)', (1, 25088), (4608, 256))
Are you loading and using the predict or that error appears if you just load the weights? What commands are you exactly using? Could you, please, post it here?
@OeslleLucena Yes, I was making a prediction when the program was throwing an exception. When loading the weight, there is no exception. I did not use the command to start running, I was running directly in the eclipse, the following is the program:
####################### Where the program starts ##################### img_width,img_height = (224,224)
def load_model(weightsPath,img_width,img_height):
#VGG-16 model
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
# Top-model for anti-spoofing
top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
model.add(top_model)
if weightsPath:
model.load_weights(weightsPath)
else:
print 'Could not load model!'
return model
def read_preprocess_image(imgPath,img_width,img_height): img = load_img(imgPath,target_size=(img_width,img_height)) imgArray = img_to_array(img) imgArray = imgArray.reshape(1,3,img_width, img_height) imgArray = imgArray/float(255)
return imgArray
if name == 'main': # load Parameters imgPath = '0129.png' #This is an image that contains a face
# read and Pre-processing image
img = read_preprocess_image(imgPath,img_width,img_height)
# load weights
model = load_model('weights/REPLAY-ftweights18.h5',img_width,img_height)
model.summary()
# predict Class
opt = optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=1e-6)
model.compile(loss='binary_crossentropy',
optimizer=opt,
metrics=['accuracy'])
outLabel = int(model.predict_classes(img,verbose=0))
print outLabel
######################### Where the program ends #######################
@okmmsky888 Now I see the problem. I forgot to mention on github but it is explained on the paper that the trained models are for size 96x96, since I did pre-processing with a face detector. So, when you upload an image with size of 224x224 it won't work due to the fact that weights from dense layers are shaped based on input size of training images. Therefore, what I recommend you is to resize you image to size of 96x96 before test it on the CNN. You could use skimage lib for that task. Here is a useful link http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.resize
@OeslleLucena I adjusted the image size to (96,96), the program is running normally. Are the corresponding labels for the two categories: 0 and 1 .0 = attack, 1 = real? I got a few photos to test, using the real photo, in the two models on the prediction results for 1. But with the attack photo (I use the phone against another mobile phone screen shot), in the 3DMAD model is predicted to 0, in the REPLAY model is predicted to 1, this result is provided with my photo scene?
@okmmsky888 I set 0 = real, 1= attack in both models, REPLAY and 3DMAD. 3DMAD is for database contained impostors using fake masks and REPLAY-ATTACK has more types of attacks. The models that I provided are for part of the databases, not all, since I needed images to test. I would say you better trust on REPLAY in this case, but remember they make mistakes too. Other thing that need to be mentioned is that you can train your own model too! Hope I answered you properly
@OeslleLucena Yes, I am going to train my own model, based on the CASIA Face Antispoofing Database and the REPLAY-ATTACK database. I am not prepared to just cut the face area, because the casia database contains some of the hands of the attacker's mobile phone, hand-held photos and other features.
@OeslleLucena I used Dlib for face alignment, cropping. Tested on your trained model, the effect does not seem to be ideal. Is it my way to have a problem?
@okmmsky888 what do you mean by "does not seem to be ideal"? is the CNN giving a wrong classification? Also, from which database this image came from? Remember that tests between different databases are a bit harder than what I've done.
@OeslleLucena Yes, CNN classification is wrong. The test image is the face image that my phone normally shoots
@okmmsky888 I mentioned before that test with different databases are not easy and what you're doing is not correct. Therefore, that result is expected .
@OeslleLucena Is it only easy to test the data from the test set of REPLAY and 3DMAD? Other data to be tested, the way is not the same? What should I do to properly test other data?
You have to do a training step using different databases to properly classify images from a different dataset and validate your system. I would recommend you to search about inter and intra databases test.
@okmmsky888 take a look at this paper http://biometrics.cse.msu.edu/Publications/Face/PatelHanJain_FaceAntispoofing_CCBR2016.pdf
@OeslleLucena Okay thank you
@okmmsky888 you're welcome!
@okmmsky888 I recently study this articles and want to achieve the text results, but there are some problems, after reading your interaction with the author, i know you get the result, so I want to ask some questions about the experiment. Will the author gives the weights can be used as a training model and then enter the processed image test? Thank you very much!
@myselfsuperhero This project has not touched for some time. If I remember correctly, the weight given by the author is the result of some data training.
@okmmsky888 the anthor in the first message said that the REPLAY-ftweights18.h5 and 3DMAD-ftweights18.h5 are already trained and they can be used directly . So if not,What should I do next? could you give me some advice about the process? i am a little confused about this.Thanks for your generous help.
I want to train my own model. I put REPLAY-ftweights18.h5 path into weights_path
argument. But I get this error: KeyError: "Can't open attribute (can't locate attribute: 'nb_layers')"
. I read it might be because of different versions of Keras. Do somebody know the solution for the problem? Or which version of Keras was used to train REPLAY-ftweights18.h5 model? Any help, please.
@AblAzhidin Same problem.... meanwhile, the following piece of code is incompatible with current version of keras(2.1.5) and tensorflow(1.6.0)
for k in range(f.attrs['nb_layers']):
if k >= len(model.layers):
# we don't look at the last (fully-connected) layers in the savefile
break
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
model.layers[k].set_weights(weights)
May use model.load_weights(filepath=weights_path, by_name=True)
instead ..
update:... Still not works...
@okmmsky888 May I wonder which version of keras and tensorflow you used in this project. Much appreciate.
@ @myselfsuperhero Sorry, I haven't touched this project for a long time. It's too difficult for me to remember now.
@AblAzhidin Sorry, I haven't touched this project for a long time. It's too difficult for me to remember now.
@yyyreal Sorry, I haven't touched this project for a long time. It's too difficult for me to remember now.
@okmmsky888 Thanks for your reply.
@yyyreal Did u get it to work?
@AblAzhidin @taewookim
I just replace the following piece of code
for k in range(f.attrs['nb_layers']):
if k >= len(model.layers):
# we don't look at the last (fully-connected) layers in the savefile
break
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
model.layers[k].set_weights(weights)
f.close()
print 'Model loaded.'
into model.load_weights(filepath=weights_path, by_name=True)
I am not sure whether I am right, but I've followed the documentation of Keras
@yyyreal 大哥,教哈我怎么用啊,急,我用不起来
@CacheTechLtd 你要怎么用???我试了试,这个基于CNN的算法对摄像头要求比较高。训练准确率很高,但是实际使用的时候,因为摄像头跟记录REPLAY-ATTACK的摄像头不一致导致数据特征分布不一样,所以效果很差么,除非自己采集数据并在相同的摄像头下面测试和运行才有比较好的准确率。