Ejemplo n.º 1
0
def create_uncertainty_model(learning_rate=1e-3, num_hidden_units=20, type = 'mobilenet_v2'):
    mu_input = Input(shape=(num_classes,))
    if type == 'mobilenet_v2':
        base_model = mobilenet_v2.MobileNetV2(include_top=False, weights='imagenet', input_tensor=None,
                                          input_shape=(224, 224, 3), pooling='avg', classes=num_classes)
    elif type == 'vgg16':
        base_model = vgg16.VGG16(include_top=False, weights='imagenet', input_tensor=None,
                                 input_shape=(224, 224, 3), pooling='avg', classes=num_classes)
    elif type == 'resnet50':
        base_model = resnet50.ResNet50(include_top=False, weights='imagenet', input_tensor=None,
                                 input_shape=(224, 224, 3), pooling='avg', classes=num_classes)
    elif type == 'vgg19':
        base_model = vgg19.VGG19(include_top=False, weights='imagenet', input_tensor=None,
                                 input_shape=(224, 224, 3), pooling='avg', classes=num_classes)
    elif type == 'inception_v3':
        base_model = inception_v3.InceptionV3(include_top=False, weights='imagenet', input_tensor=None,
                                 input_shape=(224, 224, 3), pooling='avg', classes=num_classes)
    else:
        base_model = mobilenet_v2.MobileNetV2(include_top=False, weights='imagenet', input_tensor=None,
                                              input_shape=(224, 224, 3), pooling='avg', classes=num_classes)
    base_model.trainable = False
    beta = base_model.output
    beta = Dense(num_hidden_units, activation='relu')(beta)
    beta = Dense(num_hidden_units, activation='relu')(beta)
    beta = Dense(num_hidden_units, activation='relu')(beta)
    # beta = Dense(num_hidden_units,activation='relu')(beta)
    beta = Dense(1, activation='sigmoid')(beta)
    output = concatenate([mu_input, beta])

    model = Model(inputs=[mu_input, base_model.input], outputs=output)
    model.compile(loss=dirichlet_aleatoric_cross_entropy,
                  optimizer=Adam(lr=learning_rate),
                  metrics=[max_beta, min_beta]
                  )
    return model
Ejemplo n.º 2
0
def create_bb_model(learning_rate=1e-3, num_hidden_units=20, num_classes=10):
    base_model = mobilenet_v2.MobileNetV2(include_top=False, weights='imagenet', input_tensor=None, input_shape=(32, 32, 3), pooling='avg', classes=num_classes)
    mu = Dense(num_hidden_units,activation='relu')(base_model.output)
    mu = BatchNormalization()(mu)
    output = Dense(num_classes, activation='softmax')(mu)
    model = Model(inputs=[base_model.input], outputs=output)
    return model
    def testMobileNetV2Fit(self):
        """Test training Keras MobileNetV2 application works w/ check numerics."""
        check_numerics_callback.enable_check_numerics()
        model = mobilenet_v2.MobileNetV2(alpha=0.1, weights=None)

        xs = np.zeros([2] + list(model.input_shape[1:]))
        ys = np.zeros([2] + list(model.output_shape[1:]))
        model.compile(optimizer="sgd", loss="categorical_crossentropy")
        epochs = 1
        history = model.fit(xs, ys, epochs=epochs, verbose=0)
        self.assertEqual(len(history.history["loss"]), epochs)
Ejemplo n.º 4
0
    def example(cls):
        from tensorflow.python.keras.applications import mobilenet_v2

        fsl_classifier = cls(
            preprocess_reshaped_image=mobilenet_v2.preprocess_input,
            encoder=mobilenet_v2.MobileNetV2(input_shape=[96, 96, 3],
                                             include_top=False,
                                             pooling="avg"),
            head=tf.keras.layers.Lambda(lambda x: cosine_similary(x[0], x[1])),
        )
        return fsl_classifier
Ejemplo n.º 5
0
    def testMobileNetV2Fit(self):
        """Test training Keras MobileNetV2 application works w/ check numerics."""
        # Use a large circular-buffer to make sure we capture all the executed ops.
        writer = dumping_callback.enable_dumping(self.dump_root,
                                                 circular_buffer_size=100000)
        model = mobilenet_v2.MobileNetV2(alpha=0.1, weights=None)

        xs = np.zeros([2] + list(model.input_shape[1:]))
        ys = np.zeros([2] + list(model.output_shape[1:]))
        model.compile(optimizer="sgd", loss="categorical_crossentropy")
        epochs = 1
        history = model.fit(xs, ys, epochs=epochs, verbose=0)
        self.assertEqual(len(history.history["loss"]), epochs)

        writer.FlushNonExecutionFiles()
        writer.FlushExecutionFiles()

        stack_frame_by_id = self._readAndCheckSourceFilesAndStackFrames()
        (context_ids, op_types,
         op_name_to_op_type) = self._readAndCheckGraphsFile(stack_frame_by_id)
        # Simply assert that graph are recorded and refrain from asserting on the
        # internal details of the Keras model.
        self.assertTrue(context_ids)
        self.assertTrue(op_types)
        self.assertTrue(op_name_to_op_type)

        if context.executing_eagerly():
            # NOTE(b/142486213): Execution of the TF function happens with
            # Session.run() in v1 graph mode, hence it doesn't get logged to the
            # .execution file.
            executed_op_types, _, _, _ = self._readAndCheckExecutionFile()
            self.assertTrue(executed_op_types)

        (op_names, _, tensor_values
         ) = self._readAndCheckGraphExecutionTracesFile(context_ids)
        executed_op_types = [
            op_name_to_op_type[op_name] for op_name in op_names
        ]
        # These are the ops that we can safely assume to have been executed during
        # the model's fit() call.
        self.assertIn("Conv2D", executed_op_types)
        self.assertIn("Relu6", executed_op_types)
        self.assertIn("Conv2DBackpropFilter", executed_op_types)
        self.assertIn("Relu6Grad", executed_op_types)
        # Under the default NO_TENSOR tensor-debug mode, the tensor_proto ought to
        # be an empty float32 tensor.
        for tensor_value in tensor_values:
            self.assertEqual(tensor_value.dtype, np.float32)
            self.assertEqual(tensor_value.shape, (0, ))
    def testMobileNetV2Fit(self):
        """Test training Keras MobileNetV2 application works w/ check numerics."""

        if test_lib.is_built_with_rocm():
            # This test passes with MIOpen Find Mode (which is the default)
            # This bug is being tracked via MLOpen Issue #2379, re-enable this
            # test once the fix for that issue is available in a ROCm release
            self.skipTest("MIOpen bug results in test failure")

        check_numerics_callback.enable_check_numerics()
        model = mobilenet_v2.MobileNetV2(alpha=0.1, weights=None)

        xs = np.zeros([2] + list(model.input_shape[1:]))
        ys = np.zeros([2] + list(model.output_shape[1:]))
        model.compile(optimizer="sgd", loss="categorical_crossentropy")
        epochs = 1
        history = model.fit(xs, ys, epochs=epochs, verbose=0)
        self.assertEqual(len(history.history["loss"]), epochs)
    def create_model(self, input_shape):
        base_model = mobilenet_v2.MobileNetV2(include_top=False,
                                              weights='imagenet',
                                              input_tensor=None,
                                              input_shape=(32, 32, 3),
                                              pooling='avg',
                                              classes=self.num_classes)
        beta = base_model.output
        beta = Dense(self.num_hidden_units, activation='relu')(beta)
        beta = Dense(self.num_hidden_units, activation='relu')(beta)
        beta = Dense(self.num_hidden_units, activation='relu')(beta)
        beta = Dense(1, activation='softplus')(beta)
        mu_input = Input(shape=(self.num_classes, ))
        output = concatenate([mu_input, beta])

        model = Model(inputs=[base_model.input, mu_input], outputs=output)
        model.compile(loss=self.dirichlet_aleatoric_cross_entropy,
                      optimizer=Adam(lr=self.learning_rate),
                      metrics=[self.max_beta, self.min_beta])
        return model
Ejemplo n.º 8
0
        skimage.transform.resize(image, new_shape, anti_aliasing=True)
        for image in stl10_x_train
    ])
    # logger.error("Store train data")
    # with open(os.path.sep.join([args.save_dir, 'stl10_x_train_resized.pkl']), 'wb') as file:
    #     pickle.dump((stl10_x_train_resized, stl10_y_train), file)
    logger.error("Resize test images")
    stl10_x_test_resized = np.array([
        skimage.transform.resize(image, new_shape, anti_aliasing=True)
        for image in stl10_x_test
    ])
    # logger.error("Store test data")
    # with open(os.path.sep.join([args.save_dir, 'stl10_x_test_resized.pkl']), 'wb') as file:
    #     pickle.dump((stl10_x_test_resized, stl10_y_test), file)
    logger.error("Load model")
    model = mobilenet_v2.MobileNetV2(weights='imagenet')
    logger.error("Load stl10_imagenet_mapping")
    with open(args.inverse_label_mapping_file, 'rb') as file:
        stl10_imagenet_mapping = pickle.load(file)

    logger.error("Predict training")
    train_preds = model.predict(stl10_x_train_resized)
    train_prob_preds = train_preds[:, stl10_imagenet_mapping[0]].sum(
        axis=1) / len(stl10_imagenet_mapping[0])
    for i in range(1, 10):
        train_prob_preds = np.hstack(
            (train_prob_preds,
             train_preds[:, stl10_imagenet_mapping[i]].sum(axis=1) /
             len(stl10_imagenet_mapping[0])))
    train_prob_preds = train_prob_preds.reshape(
        (num_classes, train_preds.shape[0])).T
Ejemplo n.º 9
0
    def testMobiletNetV2Fit(self, tensor_debug_mode):
        """Test training Keras MobileNetV2 works with dumping."""
        # Use a large circular-buffer to make sure we capture all the executed ops.
        writer = dumping_callback.enable_dump_debug_info(
            self.dump_root,
            tensor_debug_mode=tensor_debug_mode,
            circular_buffer_size=100000)
        model = mobilenet_v2.MobileNetV2(input_shape=(32, 32, 3),
                                         alpha=0.1,
                                         weights=None)
        y = model.layers[22].output
        y = core.Flatten()(y)
        y = core.Dense(1)(y)
        model = models.Model(inputs=model.inputs, outputs=y)

        batch_size = 2
        xs = np.zeros([batch_size] + list(model.input_shape[1:]))
        ys = np.zeros([batch_size] + list(model.output_shape[1:]))
        model.compile(optimizer="sgd", loss="mse")
        epochs = 1
        history = model.fit(xs, ys, epochs=epochs, verbose=0)
        self.assertLen(history.history["loss"], epochs)

        writer.FlushNonExecutionFiles()
        writer.FlushExecutionFiles()

        stack_frame_by_id = self._readAndCheckSourceFilesAndStackFrames()
        (context_ids, op_types, op_name_to_op_type,
         _) = self._readAndCheckGraphsFile(stack_frame_by_id)
        # Simply assert that graph are recorded and refrain from asserting on the
        # internal details of the Keras model.
        self.assertTrue(context_ids)
        self.assertTrue(op_types)
        self.assertTrue(op_name_to_op_type)

        if context.executing_eagerly():
            # NOTE(b/142486213): Execution of the TF function happens with
            # Session.run() in v1 graph mode, hence it doesn't get logged to the
            # .execution file.
            executed_op_types, _, _, _, _ = self._readAndCheckExecutionFile()
            self.assertTrue(executed_op_types)

        (op_names, _, _, tensor_values
         ) = self._readAndCheckGraphExecutionTracesFile(context_ids)
        executed_op_types = [
            op_name_to_op_type[op_name] for op_name in op_names
        ]
        # These are the ops that we can safely assume to have been executed during
        # the model's fit() call.
        self.assertIn("Conv2D", executed_op_types)
        self.assertIn("Relu6", executed_op_types)
        self.assertIn("Conv2DBackpropFilter", executed_op_types)
        self.assertIn("Relu6Grad", executed_op_types)
        if tensor_debug_mode == "NO_TENSOR":
            # Under the default NO_TENSOR tensor-debug mode, the tensor_proto ought to
            # be an empty float32 tensor.
            for tensor_value in tensor_values:
                self.assertEqual(tensor_value.dtype, np.float32)
                self.assertEqual(tensor_value.shape, (0, ))
        elif tensor_debug_mode == "FULL_TENSOR":
            conv2d_values = [
                tensor_values[i] for i, op_type in enumerate(executed_op_types)
                if op_type == "Conv2D"
            ]
            self.assertTrue(conv2d_values)
            for conv2d_value in conv2d_values:
                self.assertGreater(len(conv2d_value.shape), 1)
                self.assertEqual(conv2d_value.shape[0], batch_size)
            relu6_values = [
                tensor_values[i] for i, op_type in enumerate(executed_op_types)
                if op_type == "Relu6"
            ]
            self.assertTrue(relu6_values)
            for relu6_value in relu6_values:
                self.assertGreater(len(relu6_value.shape), 1)
                self.assertEqual(relu6_value.shape[0], batch_size)
            conv2d_bp_filter_values = [
                tensor_values[i] for i, op_type in enumerate(executed_op_types)
                if op_type == "Conv2DBackpropFilter"
            ]
            self.assertTrue(conv2d_bp_filter_values)
            for conv2d_bp_filter_value in conv2d_bp_filter_values:
                self.assertGreater(len(conv2d_bp_filter_value.shape), 1)
            relu6_grad_values = [
                tensor_values[i] for i, op_type in enumerate(executed_op_types)
                if op_type == "Relu6Grad"
            ]
            self.assertTrue(relu6_grad_values)
            for relu6_grad_value in relu6_grad_values:
                self.assertGreater(len(relu6_grad_value.shape), 1)
Ejemplo n.º 10
0
def model_setting():
    if not os.path.isdir('.\\h5'):
        os.makedirs('.\\h5')

    # whether load pre-trained model
    if INITIAL_EPOCH != 0:
        model = tf.keras.models.load_model('.\\h5\\' + '%s_%d.h5' %
                                           (PROJECT_NAME, INITIAL_EPOCH))
    else:
        if MODEL_TYPE == 'DashNet':
            from model.dashnet import DashNet
            model = DashNet(input_shape=(MODEL_HEIGHT, MODEL_WIDTH,
                                         MODEL_CHANNEL),
                            pooling='avg',
                            classes=CLASSES_NUM)

        elif MODEL_TYPE == 'ShuffleNet':
            from model.shufflenet import ShuffleNet
            model = ShuffleNet(input_shape=(MODEL_HEIGHT, MODEL_WIDTH,
                                            MODEL_CHANNEL),
                               pooling='avg',
                               classes=CLASSES_NUM,
                               groups=1)

        elif MODEL_TYPE == 'SqueezeNet':
            from model.squeezenet import SqueezeNet
            model = SqueezeNet(
                input_shape=(MODEL_HEIGHT, MODEL_WIDTH, MODEL_CHANNEL),
                pooling='avg',
                classes=CLASSES_NUM,
                weights=None,
            )

        elif MODEL_TYPE == 'Xception':
            model = xception.Xception(input_shape=(MODEL_HEIGHT, MODEL_WIDTH,
                                                   MODEL_CHANNEL),
                                      pooling='avg',
                                      include_top=True,
                                      weights=None,
                                      input_tensor=None,
                                      classes=CLASSES_NUM)

        elif MODEL_TYPE == 'NASNet':
            model = nasnet.NASNetMobile(input_shape=(MODEL_HEIGHT, MODEL_WIDTH,
                                                     MODEL_CHANNEL),
                                        pooling='avg',
                                        include_top=True,
                                        weights=None,
                                        input_tensor=None,
                                        classes=CLASSES_NUM)

        elif MODEL_TYPE == 'Mobilenet':
            model = mobilenet_v2.MobileNetV2(input_shape=(MODEL_HEIGHT,
                                                          MODEL_WIDTH,
                                                          MODEL_CHANNEL),
                                             pooling='avg',
                                             include_top=True,
                                             weights=None,
                                             input_tensor=None,
                                             classes=CLASSES_NUM,
                                             alpha=1.0)

        elif MODEL_TYPE == 'InceptionV3':
            model = inception_v3.InceptionV3(input_shape=(MODEL_HEIGHT,
                                                          MODEL_WIDTH,
                                                          MODEL_CHANNEL),
                                             pooling='avg',
                                             include_top=True,
                                             weights=None,
                                             input_tensor=None,
                                             classes=CLASSES_NUM)

        else:
            print(
                'Load Model Error!!! Please make sure you enter the correct model name!!!'
            )

    return model
Ejemplo n.º 11
0



early_stopping = EarlyStopping(monitor="val_acc", patience=2, verbose=1, mode="max")
checkpoint_path=root+"results/checkpoints.hdf5"
checkpoint = ModelCheckpoint(checkpoint_path, period=1, monitor="val_acc", verbose=1, save_best_only=True, mode="max")




#***************************************Mobilenet part****************


if network=="mobilenet":
    base_model = mobilenet_v2.MobileNetV2(input_shape=(128, 128, 3), include_top=True, weights='imagenet')



    x = base_model.output
    x = Dense(1024, activation='relu')(x)
    # and a logistic because we only have 3 classes
    predictions = Dense(3, activation='sigmoid')(x)

    model = Model(inputs=base_model.input, outputs=predictions)

    # first: train only the top layers (which were randomly initialized)
    # i.e. freeze all convolutional InceptionV3 layers
    for layer in base_model.layers:
        layer.trainable = False