Example #1
0
    def __init__(self,
                 cfg,
                 weight_decay=0.0001,
                 data_format='NHWC',
                 reuse=None,
                 images_ph=None,
                 lbls_ph=None):

        self.cfg = cfg
        num_classes = cfg.num_classes
        batch_size = None
        if lbls_ph is not None:
            self.gt_lbls = tf.reshape(lbls_ph, [-1, num_classes])
        else:
            self.gt_lbls = tf.placeholder(tf.int32,
                                          shape=(batch_size, num_classes),
                                          name='class_lbls')

        self.do_augmentation = tf.placeholder(tf.bool, name='do_augmentation')
        self.loss_class_weight = tf.placeholder(tf.float32,
                                                shape=(num_classes,
                                                       num_classes),
                                                name='weights')
        if cfg.db_name == 'honda':
            self.input = tf.placeholder(tf.float32,
                                        shape=(batch_size, const.frame_height,
                                               const.frame_width,
                                               const.context_channels),
                                        name='context_input')
        else:
            self.input = tf.placeholder(
                tf.float32,
                shape=(batch_size, const.max_frame_size, const.max_frame_size,
                       const.frame_channels),
                name='context_input')

        # if is_training:
        if images_ph is not None:
            self.input = images_ph
            _, w, h, c = self.input.shape
            aug_imgs = tf.reshape(self.input, [-1, w, h, 3])
            print('No nnutils Augmentation')
        else:
            if cfg.db_name == 'honda':
                aug_imgs = self.input
            else:
                aug_imgs = tf.cond(
                    self.do_augmentation,
                    lambda: batch_augment.augment(self.input,
                                                  cfg.preprocess_func,
                                                  horizontal_flip=True,
                                                  vertical_flip=False,
                                                  rotate=0,
                                                  crop_probability=0,
                                                  color_aug_probability=0),
                    lambda: batch_augment.center_crop(self.input, cfg.
                                                      preprocess_func))
            # aug_imgs = self.input ## Already augmented

            # else:
        #     if images_ph is not None:
        #         self.input = images_ph
        #         _,w,h,c = self.input.shape
        #         aug_imgs = tf.reshape(self.input, [-1, w, h, 3])
        #         print('No nnutils Augmentation')
        #     else:
        #         # self.input = tf.placeholder(tf.float32, shape=(batch_size, const.frame_height, const.frame_width,
        #         #                                                const.frame_channels), name='context_input')
        #         aug_imgs = tf.cond(self.is_training,
        #                            lambda: nn_utils.augment(self.input, horizontal_flip=True, vertical_flip=False,
        #                                                     rotate=0, crop_probability=0, color_aug_probability=0)
        #                            , lambda: nn_utils.center_crop(self.input))

        with tf.contrib.slim.arg_scope(
                densenet_arg_scope(weight_decay=weight_decay,
                                   data_format=data_format)):
            nets, train_end_points = densenet(
                aug_imgs,
                num_classes=num_classes,
                reduction=0.5,
                growth_rate=48,
                num_filters=96,
                num_layers=[6, 12, 36, 24],
                data_format=data_format,
                is_training=True,  ## Set is always to false
                reuse=None,
                scope='densenet161')

            val_nets, val_end_points = densenet(
                aug_imgs,
                num_classes=num_classes,
                reduction=0.5,
                growth_rate=48,
                num_filters=96,
                num_layers=[6, 12, 36, 24],
                data_format=data_format,
                is_training=False,  ## Set is always to false
                reuse=True,
                scope='densenet161')

        def cal_metrics(end_points):
            gt = tf.argmax(self.gt_lbls, 1)
            logits = tf.reshape(end_points['densenet161/logits'],
                                [-1, num_classes])
            pre_logits = end_points['densenet161/dense_block4']

            center_supervised_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
                labels=self.gt_lbls, logits=logits, name='xentropy_center')
            loss = tf.reduce_mean(center_supervised_cross_entropy,
                                  name='xentropy_mean')
            predictions = tf.reshape(end_points['predictions'],
                                     [-1, num_classes])
            class_prediction = tf.argmax(predictions, 1)
            supervised_correct_prediction = tf.equal(gt, class_prediction)
            supervised_correct_prediction_cast = tf.cast(
                supervised_correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(supervised_correct_prediction_cast)
            confusion_mat = tf.confusion_matrix(gt,
                                                class_prediction,
                                                num_classes=num_classes)
            _, accumulated_accuracy = tf.metrics.accuracy(gt, class_prediction)
            _, per_class_acc_acc = tf.metrics.mean_per_class_accuracy(
                gt, class_prediction, num_classes=num_classes)
            per_class_acc_acc = tf.reduce_mean(per_class_acc_acc)

            return loss, pre_logits, accuracy, confusion_mat, accumulated_accuracy, per_class_acc_acc

        self.train_loss, self.train_pre_logits, self.train_accuracy, self.train_confusion_mat, self.train_accumulated_accuracy, self.train_per_class_acc_acc = cal_metrics(
            train_end_points)
        self.val_loss, self.val_pre_logits, self.val_accuracy, self.val_confusion_mat, self.val_accumulated_accuracy, self.val_per_class_acc_acc = cal_metrics(
            val_end_points)
Example #2
0
    def dataset_from_files(self,
                           train_imgs,
                           train_lbls,
                           is_training,
                           cfg,
                           repeat=True,
                           shuffle=False):
        def _parse_function(filename, label):
            print(filename)
            image_string = tf.read_file(filename)
            #image_string = tf.Print(image_string,[filename,label],'img name ')
            image_decoded = tf.image.decode_jpeg(image_string, channels=3)
            # print(image_decoded.dtype)
            #image_decoded = tf.Print(image_decoded, [tf.shape(image_decoded)], 'shape ::')
            # image_resized = tf.image.resize_images(image_decoded, [const.max_frame_size, const.max_frame_size])
            # image_decoded = tf.image.resize_images(image_decoded, [const.max_frame_size, const.max_frame_size])
            # image_decoded = tf.cast(image_decoded, tf.uint8)

            return image_decoded, tf.one_hot(label,
                                             cfg.num_classes,
                                             dtype=tf.int64)

        filenames = tf.constant(train_imgs)
        labels = tf.constant(train_lbls, dtype=tf.int32)

        dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))

        if shuffle:
            print('Will shuffle dataset ***')
            dataset = dataset.shuffle(len(train_imgs))

        if repeat:
            # dataset = dataset.shuffle(len(train_imgs))
            dataset = dataset.repeat(
                None)  # Repeat forever. Funny way of stating it.
        else:
            dataset = dataset.repeat(1)  # No Repeat

        dataset = dataset.map(_parse_function,
                              num_parallel_calls=cfg.tuple_loader_queue_size)

        # preprocess_mod = locate(config.preprocessing_module)
        # func_name = preprocess_mod.preprocess_for_train_simple
        # if not is_training:
        #     func_name = preprocess_mod.preprocess_for_eval_simple
        # dataset = dataset.map(lambda im, lbl: (func_name (im,const.frame_height,const.frame_width), lbl))

        batch_size = cfg.batch_size

        if is_training:
            if cfg.aug_style == 'batch':
                dataset = dataset.batch(batch_size)
                dataset = dataset.map(lambda im_batch, lbl_batch: (
                    batch_augment.augment(im_batch,
                                          cfg.preprocess_func,
                                          horizontal_flip=True,
                                          vertical_flip=False,
                                          rotate=0,
                                          crop_probability=0,
                                          color_aug_probability=0), lbl_batch))
            elif cfg.aug_style == 'img':
                dataset = dataset.map(
                    lambda im, lbl: (img_augment.preprocess_for_train(
                        im,
                        cfg.frame_size,
                        cfg.frame_size,
                        preprocess_func=cfg.preprocess_func), lbl))
                dataset = dataset.batch(batch_size)
        else:
            if cfg.aug_style == 'batch':

                dataset = dataset.batch(batch_size)
                dataset = dataset.map(
                    lambda im_batch, lbl_batch: (batch_augment.center_crop(
                        im_batch, cfg.preprocess_func), lbl_batch))
            elif cfg.aug_style == 'img':
                dataset = dataset.map(
                    lambda im, lbl: (img_augment.preprocess_for_eval(
                        im,
                        cfg.frame_size,
                        cfg.frame_size,
                        preprocess_func=cfg.preprocess_func), lbl))
                dataset = dataset.batch(batch_size)

        dataset = dataset.prefetch(1)
        return dataset
Example #3
0
    def __init__(self,
                 cfg=None,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 reuse=None,
                 scope='InceptionV4',
                 create_aux_logits=True,
                 images_ph=None,
                 lbls_ph=None):
        self.cfg = cfg
        batch_size = None
        num_classes = cfg.num_classes
        if lbls_ph is not None:
            self.gt_lbls = tf.reshape(lbls_ph, [-1, num_classes])
        else:
            self.gt_lbls = tf.placeholder(tf.int32,
                                          shape=(batch_size, num_classes),
                                          name='class_lbls')

        self.do_augmentation = tf.placeholder(tf.bool, name='do_augmentation')
        self.loss_class_weight = tf.placeholder(tf.float32,
                                                shape=(num_classes,
                                                       num_classes),
                                                name='weights')
        if cfg.db_name == 'honda':
            self.input = tf.placeholder(tf.float32,
                                        shape=(batch_size, const.frame_height,
                                               const.frame_width,
                                               const.context_channels),
                                        name='context_input')
        else:
            self.input = tf.placeholder(
                tf.float32,
                shape=(batch_size, const.max_frame_size, const.max_frame_size,
                       const.frame_channels),
                name='context_input')

        # if is_training:
        if images_ph is not None:
            self.input = images_ph
            _, w, h, c = self.input.shape
            aug_imgs = tf.reshape(self.input, [-1, w, h, 3])
            print('No nnutils Augmentation')
        else:
            if cfg.db_name == 'honda':
                aug_imgs = self.input
            else:
                aug_imgs = tf.cond(
                    self.do_augmentation,
                    lambda: batch_augment.augment(self.input,
                                                  cfg.preprocess_func,
                                                  horizontal_flip=True,
                                                  vertical_flip=False,
                                                  rotate=0,
                                                  crop_probability=0,
                                                  color_aug_probability=0),
                    lambda: batch_augment.center_crop(self.input, cfg.
                                                      preprocess_func))

        with slim.arg_scope(inception_v4_arg_scope()):
            _, train_end_points = inception_v4(
                aug_imgs,
                num_classes,
                dropout_keep_prob=dropout_keep_prob,
                create_aux_logits=create_aux_logits,
                is_training=True,
                reuse=reuse,
                scope=scope)

            _, val_end_points = inception_v4(
                aug_imgs,
                num_classes,
                dropout_keep_prob=dropout_keep_prob,
                create_aux_logits=create_aux_logits,
                is_training=False,
                reuse=True,
                scope=scope)

        def cal_metrics(end_points):
            gt = tf.argmax(self.gt_lbls, 1)
            logits = tf.reshape(end_points['Logits'], [-1, num_classes])
            pre_logits = end_points['Mixed_6h']

            center_supervised_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
                labels=self.gt_lbls, logits=logits, name='xentropy_center')
            loss = tf.reduce_mean(center_supervised_cross_entropy,
                                  name='xentropy_mean')
            predictions = tf.reshape(end_points['Predictions'],
                                     [-1, num_classes])
            class_prediction = tf.argmax(predictions, 1)
            supervised_correct_prediction = tf.equal(gt, class_prediction)
            supervised_correct_prediction_cast = tf.cast(
                supervised_correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(supervised_correct_prediction_cast)
            confusion_mat = tf.confusion_matrix(gt,
                                                class_prediction,
                                                num_classes=num_classes)
            _, accumulated_accuracy = tf.metrics.accuracy(gt, class_prediction)
            _, per_class_acc_acc = tf.metrics.mean_per_class_accuracy(
                gt, class_prediction, num_classes=num_classes)
            per_class_acc_acc = tf.reduce_mean(per_class_acc_acc)
            return loss, pre_logits, accuracy, confusion_mat, accumulated_accuracy, per_class_acc_acc, class_prediction

        self.train_loss,self.train_pre_logits,self.train_accuracy,self.train_confusion_mat,\
                        self.train_accumulated_accuracy,self.train_per_class_acc_acc ,self.train_class_prediction = cal_metrics(train_end_points)


        self.val_loss,self.val_pre_logits,self.val_accuracy, self.val_confusion_mat,\
                        self.val_accumulated_accuracy,self.val_per_class_acc_acc ,self.val_class_prediction = cal_metrics(val_end_points)
    def __init__(self,
                 cfg=None,
                 is_training=True,
                 global_pool=True,
                 output_stride=None,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='resnet_v2_50',
                 images_ph=None,
                 lbls_ph=None):
        self.cfg = cfg
        batch_size = None
        if lbls_ph is not None:
            self.gt_lbls = tf.reshape(lbls_ph, [-1, cfg.num_classes])
        else:
            self.gt_lbls = tf.placeholder(tf.int32,
                                          shape=(batch_size, cfg.num_classes),
                                          name='class_lbls')

        self.do_augmentation = tf.placeholder(tf.bool, name='do_augmentation')
        self.loss_class_weight = tf.placeholder(tf.float32,
                                                shape=(cfg.num_classes,
                                                       cfg.num_classes),
                                                name='weights')
        if 'honda' in cfg.db_name:
            self.input = tf.placeholder(tf.float32,
                                        shape=(batch_size, const.frame_height,
                                               const.frame_width,
                                               const.context_channels),
                                        name='context_input')
        else:
            self.input = tf.placeholder(
                tf.float32,
                shape=(batch_size, const.max_frame_size, const.max_frame_size,
                       const.frame_channels),
                name='context_input')

        # if is_training:
        if images_ph is not None:
            self.input = images_ph
            _, w, h, c = self.input.shape
            aug_imgs = tf.reshape(self.input, [-1, w, h, 3])
            print('No nnutils Augmentation')
        else:
            if 'honda' in cfg.db_name:
                aug_imgs = self.input
            else:
                aug_imgs = tf.cond(
                    self.do_augmentation,
                    lambda: batch_augment.augment(self.input,
                                                  cfg.preprocess_func,
                                                  horizontal_flip=True,
                                                  vertical_flip=False,
                                                  rotate=0,
                                                  crop_probability=0,
                                                  color_aug_probability=0),
                    lambda: batch_augment.center_crop(self.input, cfg.
                                                      preprocess_func))

        with slim.arg_scope(resnet_arg_scope()):
            _, train_end_points = resnet_v2_50(aug_imgs,
                                               cfg.num_classes,
                                               is_training=True,
                                               global_pool=global_pool,
                                               output_stride=output_stride,
                                               spatial_squeeze=spatial_squeeze,
                                               reuse=reuse,
                                               scope=scope)

            _, val_end_points = resnet_v2_50(aug_imgs,
                                             cfg.num_classes,
                                             is_training=False,
                                             global_pool=global_pool,
                                             output_stride=output_stride,
                                             spatial_squeeze=spatial_squeeze,
                                             reuse=True,
                                             scope=scope)

            _, val_end_features = resnet_v2_50(aug_imgs,
                                               0,
                                               is_training=False,
                                               global_pool=global_pool,
                                               output_stride=output_stride,
                                               spatial_squeeze=spatial_squeeze,
                                               reuse=True,
                                               scope=scope)

        def cal_metrics(end_points):
            gt = tf.argmax(self.gt_lbls, 1)
            logits = tf.reshape(end_points['resnet_v2_50/logits'],
                                [-1, cfg.num_classes])
            pre_logits = end_points['resnet_v2_50/block4/unit_3/bottleneck_v2']

            center_supervised_cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
                labels=self.gt_lbls, logits=logits, name='xentropy_center')
            loss = tf.reduce_mean(center_supervised_cross_entropy,
                                  name='xentropy_mean')
            predictions = tf.reshape(end_points['predictions'],
                                     [-1, cfg.num_classes])
            class_prediction = tf.argmax(predictions, 1)
            supervised_correct_prediction = tf.equal(gt, class_prediction)
            supervised_correct_prediction_cast = tf.cast(
                supervised_correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(supervised_correct_prediction_cast)
            confusion_mat = tf.confusion_matrix(gt,
                                                class_prediction,
                                                num_classes=cfg.num_classes)
            _, accumulated_accuracy = tf.metrics.accuracy(gt, class_prediction)
            _, per_class_acc_acc = tf.metrics.mean_per_class_accuracy(
                gt, class_prediction, num_classes=cfg.num_classes)
            per_class_acc_acc = tf.reduce_mean(per_class_acc_acc)

            #top_3= tf.math.in_top_k(gt,predictions,3)
            #supervised_correct_prediction_cast_3 = tf.cast(top_3, tf.float32)
            #accuracy_3 = tf.reduce_mean(supervised_correct_prediction_cast_3)

            return loss, pre_logits, accuracy, confusion_mat, accumulated_accuracy, per_class_acc_acc, class_prediction, gt, predictions  #,accuracy_3



        self.train_loss,self.train_pre_logits,self.train_accuracy,self.train_confusion_mat,\
                        self.train_accumulated_accuracy,self.train_per_class_acc_acc,self.train_class_prediction,self.train_gt,self.train_preds  = cal_metrics(train_end_points)


        self.val_loss,self.val_pre_logits,self.val_accuracy, self.val_confusion_mat,\
                        self.val_accumulated_accuracy,self.val_per_class_acc_acc,self.val_class_prediction,self.val_gt,self.val_preds   = cal_metrics(val_end_points)

        self.val_end_features, self.val_features_labels = val_end_features, tf.argmax(
            self.gt_lbls, 1)
    def __init__(self,
                 cfg,
                 weight_decay=0.0001,
                 data_format='NHWC',
                 is_training=False,
                 reuse=None,
                 images_ph=None,
                 lbls_ph=None,
                 weights_ph=None):

        self.cfg = cfg
        num_classes = cfg.num_classes
        batch_size = None
        if lbls_ph is not None:
            self.gt_lbls = tf.reshape(lbls_ph, [-1, cfg.num_classes])
        else:
            self.gt_lbls = tf.placeholder(tf.int32,
                                          shape=(batch_size, cfg.num_classes),
                                          name='class_lbls')

        self.do_augmentation = tf.placeholder(tf.bool, name='do_augmentation')
        self.loss_class_weight = tf.placeholder(tf.float32,
                                                shape=(cfg.num_classes,
                                                       cfg.num_classes),
                                                name='weights')
        if cfg.db_name == 'honda':
            self.input = tf.placeholder(tf.float32,
                                        shape=(batch_size, const.frame_height,
                                               const.frame_width,
                                               const.context_channels),
                                        name='context_input')
        else:
            self.input = tf.placeholder(
                tf.float32,
                shape=(batch_size, const.max_frame_size, const.max_frame_size,
                       const.frame_channels),
                name='context_input')

        # if is_training:
        if images_ph is not None:
            self.input = images_ph
            _, w, h, c = self.input.shape
            aug_imgs = tf.reshape(self.input, [-1, w, h, 3])
            print('No nnutils Augmentation')
        else:
            if cfg.db_name == 'honda':
                aug_imgs = self.input
            else:
                aug_imgs = tf.cond(
                    self.do_augmentation,
                    lambda: batch_augment.augment(self.input,
                                                  cfg.preprocess_func,
                                                  horizontal_flip=True,
                                                  vertical_flip=False,
                                                  rotate=0,
                                                  crop_probability=0,
                                                  color_aug_probability=0),
                    lambda: batch_augment.center_crop(self.input, cfg.
                                                      preprocess_func))

        with tf.contrib.slim.arg_scope(
                mobilenet_v1_arg_scope(is_training=is_training,
                                       weight_decay=weight_decay)):
            _, train_end_points = mobilenet_v1(
                aug_imgs,
                num_classes=num_classes,
                dropout_keep_prob=0.999,
                is_training=True,
                min_depth=8,
                depth_multiplier=1.0,
                conv_defs=None,
                prediction_fn=tf.contrib.layers.softmax,
                spatial_squeeze=True,
                reuse=None,
                scope='MobilenetV1',
                global_pool=True)

            _, val_end_points = mobilenet_v1(
                aug_imgs,
                num_classes=num_classes,
                dropout_keep_prob=0.999,
                is_training=False,
                min_depth=8,
                depth_multiplier=1.0,
                conv_defs=None,
                prediction_fn=tf.contrib.layers.softmax,
                spatial_squeeze=True,
                reuse=True,
                scope='MobilenetV1',
                global_pool=True)

        def cal_metrics(end_points):
            gt = tf.argmax(self.gt_lbls, 1)
            logits = tf.reshape(end_points['Logits'], [-1, num_classes])
            pre_logits = end_points['Conv2d_13_pointwise']

            center_supervised_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
                labels=self.gt_lbls, logits=logits, name='xentropy_center')
            loss = tf.reduce_mean(center_supervised_cross_entropy,
                                  name='xentropy_mean')
            predictions = tf.reshape(end_points['Predictions'],
                                     [-1, cfg.num_classes])
            class_prediction = tf.argmax(predictions, 1)
            supervised_correct_prediction = tf.equal(gt, class_prediction)
            supervised_correct_prediction_cast = tf.cast(
                supervised_correct_prediction, tf.float32)
            accuracy = tf.reduce_mean(supervised_correct_prediction_cast)
            confusion_mat = tf.confusion_matrix(gt,
                                                class_prediction,
                                                num_classes=cfg.num_classes)
            _, accumulated_accuracy = tf.metrics.accuracy(gt, class_prediction)
            _, per_class_acc_acc = tf.metrics.mean_per_class_accuracy(
                gt, class_prediction, num_classes=cfg.num_classes)
            per_class_acc_acc = tf.reduce_mean(per_class_acc_acc)
            return loss, pre_logits, accuracy, confusion_mat, accumulated_accuracy, per_class_acc_acc

        self.train_loss,self.train_pre_logits,self.train_accuracy,self.train_confusion_mat,\
                        self.train_accumulated_accuracy,self.train_per_class_acc_acc  = cal_metrics(train_end_points)


        self.val_loss,self.val_pre_logits,self.val_accuracy, self.val_confusion_mat,\
                        self.val_accumulated_accuracy,self.val_per_class_acc_acc  = cal_metrics(val_end_points)