Giter Site home page Giter Site logo

brucelike / keras-center-loss-mnist Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mjdelta/keras-center-loss-mnist

0.0 0.0 0.0 691 KB

keras implementation of A Discriminative Feature Learning Approach for Deep Face Recognition based on MNIST

Jupyter Notebook 100.00%

keras-center-loss-mnist's Introduction

keras-center-loss-MNIST

MNIST implementation of A Discriminative Feature Learning Approach for Deep Face Recognition.

Purpose

There are two main implementation of center loss by keras online. One takes use of Embedding Layer, automatically computing the centers. Another takes use of self-defined layer, computing and updating the centers with self-defined equations strictly based on what the paper purposes in 'A Discriminative Feature Learning Approach for Deep Face Recognition'. So I implement both to compare the performance. Here are the details.

Joint Supervision Loss

Alt Joint Supervision Loss

Backwarding of Centers

Alt Backwarding of Centers

Embedding Results

input_=Input(shape=(1,))
centers=Embedding(10,2)(input_)
intra_loss=Lambda(lambda x:K.sum(K.square(x[0]-x[1][:,0]),1,keepdims=True))([out1,centers])
model_center_loss=Model([inputs,input_],[out2,intra_loss])
model_center_loss.compile(optimizer="sgd",
                          loss=["categorical_crossentropy",lambda y_true,y_pred:y_pred],
                          loss_weights=[1,lambda_c/2.],
                          metrics=["acc"])

More details, please refer to the code in this repo.

Self-defined Results

class CenterLossLayer(Layer):
    def __init__(self,alpha=0.5,**kwargs):
        super().__init__(**kwargs)
        self.alpha=alpha
    def build(self,input_shape):
        self.centers=self.add_weight(name="centers",
                                    shape=(10,2),
                                    initializer="uniform",
                                    trainable=False)
        super().build(input_shape)
    def call(self,x,mask=None):
        #x[0]:N*2 x[1]:N*10 centers:10*2
        delta_centers=K.dot(K.transpose(x[1]),K.dot(x[1],self.centers)-x[0])
        centers_count=K.sum(K.transpose(x[1]),axis=-1,keepdims=True)+1
        delta_centers/=centers_count
        new_centers=self.centers-self.alpha*delta_centers
        self.add_update((self.centers,new_centers),x)
        
        self.result=x[0]-K.dot(x[1],self.centers)
        self.result=K.sum(self.result**2,axis=1,keepdims=True)
        return self.result
    def compute_output_shape(self,input_shape):
        return K.int_shape(self.result)
input_=Input((10,))
center_loss=CenterLossLayer()([out1,input_])

model_center_loss=Model([inputs,input_],[out2,center_loss])
model_center_loss.compile(optimizer="sgd",
                          loss=["categorical_crossentropy",lambda y_true,y_pred:y_pred],
                          loss_weights=[1,lambda_c/2.],
                          metrics=["acc"])

More details, please refer to the code in this repo.

keras-center-loss-mnist's People

Contributors

mjdelta avatar

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.