コード例 #1
0
ファイル: classifier_FC_Keras.py プロジェクト: gmnamra/tools
    def init_model(self, input_shape, num_classes):  #_alexNet

        n_filters = 96

        self.model = Sequential()
        self.model.add(BatchNormalization(input_shape=input_shape))

        self.model.add(
            Conv2D(n_filters, (11, 11),
                   activation='relu',
                   strides=(2, 2),
                   padding='same',
                   data_format='channels_last'))
        self.model.add(MaxPooling2D((2, 2)))

        N = self.model.layers[-1].output.get_shape().as_list()[3]
        self.model.add(
            Conv2D(N, (5, 5),
                   activation='relu',
                   strides=(2, 2),
                   padding='same',
                   data_format='channels_last'))
        self.model.add(MaxPooling2D((2, 2), strides=(2, 2)))

        N = self.model.layers[-1].output.get_shape().as_list()[3]
        self.model.add(
            Conv2D(N, (3, 3),
                   activation='relu',
                   strides=(1, 1),
                   padding='same',
                   data_format='channels_last'))
        self.model.add(MaxPooling2D((2, 2), strides=(2, 2)))

        self.model.add(Flatten())
        self.model.add(Dropout(0.5))
        self.model.add(
            Dense(numpy.prod(
                self.model.layers[-3].output.get_shape().as_list()[1:]),
                  activation='relu'))
        self.model.add(Dense(num_classes, activation='softmax'))

        weights = tools_CNN_view.import_weight(
            'data/filter1.png', (11, 11, 3, 96))[:, :, :, :n_filters]
        self.model.layers[1].set_weights(
            [weights, tools_CNN_view.import_bias()])
        self.model.layers[1].trainable = True

        self.model.summary()
        self.model.compile(optimizer='adam',
                           loss='categorical_crossentropy',
                           metrics=['accuracy'])
        tools_CNN_view.visualize_filters(self.model, self.folder_debug)
        tools_CNN_view.visualize_layers(self.model, self.filename_debug_image,
                                        self.folder_debug)

        return
コード例 #2
0
ファイル: classifier_FC_Keras.py プロジェクト: gmnamra/tools
    def init_model_2x2(self, input_shape, num_classes):

        n_filters = 64
        n_features = 1 * num_classes * num_classes
        n_features = 64

        scale = int(
            math.sqrt(n_filters) *
            math.sqrt(input_shape[0] * input_shape[1] / n_features))
        d1 = int(math.sqrt(scale))
        d2 = int(scale / d1)

        self.S1 = int(math.sqrt(d1))
        self.S2 = int(d1 / self.S1)

        self.S3 = int(math.sqrt(d2))
        self.S4 = int(d2 / self.S3)

        self.model = Sequential()
        self.model.add(BatchNormalization(input_shape=input_shape))

        self.model.add(
            Conv2D(n_filters, (2, 2),
                   activation='relu',
                   strides=(self.S1, self.S1),
                   padding='same',
                   data_format='channels_last'))
        self.model.add(MaxPooling2D((2, 2), strides=(self.S2, self.S2)))

        N = self.model.layers[-1].output.get_shape().as_list()[3]
        self.model.add(
            Conv2D(N, (2, 2),
                   activation='relu',
                   strides=(self.S3, self.S3),
                   padding='same',
                   data_format='channels_last'))
        self.model.add(MaxPooling2D((2, 2), strides=(self.S4, self.S4)))

        self.model.add(Flatten())
        self.model.add(Dropout(0.5))
        N = numpy.prod(self.model.layers[-3].output.get_shape().as_list()[1:])
        self.model.add(Dense(N, activation='relu'))
        self.model.add(Dense(num_classes, activation='softmax'))
        self.model.compile(optimizer='adam',
                           loss='categorical_crossentropy',
                           metrics=['accuracy'])

        tools_CNN_view.visualize_layers(self.model, self.filename_debug_image,
                                        self.folder_debug)
        self.model.summary()
        return
コード例 #3
0
def visualize_layers_keras_MobileNet():

    filename_image = 'data/ex_natural_images/dog/dog_0101.jpg'
    path_out = 'data/output/'

    if not os.path.exists(path_out):
        os.makedirs(path_out)
    else:
        tools_IO.remove_files(path_out)

    CNN = CNN_App_Keras.CNN_App_Keras()
    tools_CNN_view.visualize_filters(CNN.model, path_out)
    tools_CNN_view.visualize_layers(CNN.model, filename_image, path_out)

    return
コード例 #4
0
def visualize_layers_detector_YOLO3():

    filename_image = 'data/ex_detector/bike/Image.png'
    path_out = 'data/output/'

    if not os.path.exists(path_out):
        os.makedirs(path_out)
    else:
        tools_IO.remove_files(path_out)

    CNN = detector_YOLO3.detector_YOLO3(
        './data/ex_yolo/MODELS/model_default.h5',
        './data/ex_YOLO/models/metadata_default.txt')

    tools_CNN_view.visualize_layers(CNN.model, filename_image, path_out)
    #tools_CNN_view.visualize_filters(CNN.model,path_out)

    return
コード例 #5
0
ファイル: classifier_FC_Keras.py プロジェクト: gmnamra/tools
    def init_model_16_24(self, input_shape, num_classes):
        n_filters = 32 * 3

        self.model = Sequential()
        self.model.add(BatchNormalization(input_shape=input_shape))

        self.model.add(
            Conv2D(n_filters, (2, 2),
                   activation='relu',
                   strides=(2, 2),
                   padding='same',
                   data_format='channels_last'))
        self.model.add(MaxPooling2D((2, 2), strides=(2, 2)))

        N = self.model.layers[-1].output.get_shape().as_list()[3]
        self.model.add(
            Conv2D(N, (2, 2),
                   activation='relu',
                   strides=(2, 2),
                   padding='same',
                   data_format='channels_last'))
        self.model.add(MaxPooling2D((2, 2), strides=(2, 2)))

        self.model.add(Flatten())
        self.model.add(Dropout(0.5))
        N = numpy.prod(self.model.layers[-3].output.get_shape().as_list()[1:])
        self.model.add(Dense(int(N / 4), activation='relu'))
        self.model.add(Dense(num_classes, activation='softmax'))
        self.model.compile(optimizer='adam',
                           loss='categorical_crossentropy',
                           metrics=['accuracy'])

        tools_CNN_view.visualize_layers(self.model, self.filename_debug_image,
                                        self.folder_debug)
        self.model.summary()
        return