예제 #1
0
def load_model(data_bunch, model_type, model_name):
    """
    Function to create and load pretrained weights of convolutional learner
    """
    learn = create_cnn(data_bunch, model_type, pretrained=False)
    learn.load(model_name)
    return learn
예제 #2
0
    def __init__(self, data, backbone=None, pretrained_path=None):
        super().__init__()

        self._device = torch.device(
            'cuda') if torch.cuda.is_available() else torch.device('cpu')

        if not HAS_FASTAI:
            _raise_fastai_import_error()

        if backbone is None:
            self._backbone = models.resnet34
        elif type(backbone) is str:
            self._backbone = getattr(models, backbone)
        else:
            self._backbone = backbone

        self._emd_template = _EMD_TEMPLATE

        self._code = feature_classifier_prf

        self._data = data
        self.learn = create_cnn(data, self._backbone, metrics=accuracy)
        self.learn.model = self.learn.model.to(self._device)

        if pretrained_path is not None:
            self.load(pretrained_path)
예제 #3
0
    def __init__(self,
                 data,
                 grids=[4, 2, 1],
                 zooms=[0.7, 1., 1.3],
                 ratios=[[1., 1.], [1., 0.5], [0.5, 1.]],
                 backbone=None,
                 drop=0.3,
                 bias=-4.,
                 focal_loss=False,
                 pretrained_path=None):

        super().__init__()

        self._device = torch.device(
            'cuda') if torch.cuda.is_available else torch.device('cpu')

        if backbone is None:
            backbone = resnet34

        self._create_anchors(grids, zooms, ratios)

        ssd_head = SSDHead(grids,
                           self._anchors_per_cell,
                           data.c,
                           drop=drop,
                           bias=bias)

        self._data = data
        self.learn = create_cnn(data=data,
                                base_arch=backbone,
                                custom_head=ssd_head)
        self.learn.model = self.learn.model.to(self._device)

        if pretrained_path is not None:
            self.load(pretrained_path)

        if focal_loss:
            self._loss_f = FocalLoss(data.c)
        else:
            self._loss_f = BCE_Loss(data.c)

        self.learn.loss_func = self._ssd_loss
예제 #4
0
def get_learner(classes):
    # TODO: Can we make this faster/lighter?
    data = ImageDataBunch.single_from_classes(".", classes, ds_tfms=get_transforms(), size=224).normalize(imagenet_stats)
    learn = create_cnn(data, resnet34, pretrained=False)
    learn.load('makemodel-392')
    return learn
예제 #5
0
    def __init__(self,
                 data,
                 grids=[4, 2, 1],
                 zooms=[0.7, 1., 1.3],
                 ratios=[[1., 1.], [1., 0.5], [0.5, 1.]],
                 backbone=None,
                 drop=0.3,
                 bias=-4.,
                 focal_loss=False,
                 pretrained_path=None,
                 location_loss_factor=None):

        super().__init__()

        self._device = torch.device(
            'cuda') if torch.cuda.is_available() else torch.device('cpu')

        # assert (location_loss_factor is not None) or ((location_loss_factor > 0) and (location_loss_factor < 1)),
        if location_loss_factor is not None:
            if not ((location_loss_factor > 0) and (location_loss_factor < 1)):
                raise Exception(
                    '`location_loss_factor` should be greater than 0 and less than 1'
                )
        self.location_loss_factor = location_loss_factor

        if not HAS_FASTAI:
            _raise_fastai_import_error()

        if backbone is None:
            self._backbone = models.resnet34
        elif type(backbone) is str:
            self._backbone = getattr(models, backbone)
        else:
            self._backbone = backbone

        self._create_anchors(grids, zooms, ratios)

        feature_sizes = model_sizes(create_body(self._backbone),
                                    size=(data.chip_size, data.chip_size))
        num_features = feature_sizes[-1][-1]
        num_channels = feature_sizes[-1][1]

        ssd_head = SSDHead(grids,
                           self._anchors_per_cell,
                           data.c,
                           num_features=num_features,
                           drop=drop,
                           bias=bias,
                           num_channels=num_channels)

        self._data = data
        self.learn = create_cnn(data=data,
                                arch=self._backbone,
                                custom_head=ssd_head)
        self.learn.model = self.learn.model.to(self._device)

        if pretrained_path is not None:
            self.load(pretrained_path)

        if focal_loss:
            self._loss_f = FocalLoss(data.c)
        else:
            self._loss_f = BCE_Loss(data.c)

        self.learn.loss_func = self._ssd_loss
예제 #6
0
def detect_facial_attributes(input_path, output_path, save_video):
    path = Path(input_path)

    # Creating a databunch
    imagenet_stats = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    data = (ImageItemList.from_csv(
        path, csv_name="labels.csv").no_split().label_from_df(
            label_delim=" ").transform(
                None,
                size=128).databunch(no_check=True).normalize(imagenet_stats))

    # Loading our model
    learn = create_cnn(data, models.resnet50, pretrained=False)
    learn.load("ff_stage-2-rn50")

    # Loading HAAR cascade
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

    cap = cv2.VideoCapture(0)
    if save_video:
        out = cv2.VideoWriter(output_path + "output.avi", -1, 20.0, (640, 480))

    while True:
        # Capture frame-by-frame
        _, frame = cap.read()

        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Find faces using Haar cascade
        face_coord = face_cascade.detectMultiScale(gray,
                                                   1.1,
                                                   5,
                                                   minSize=(30, 30))

        ## Looping through each face
        for coords in face_coord:

            ## Finding co-ordinates of face
            X, Y, w, h = coords

            ## Finding frame size
            H, W, _ = frame.shape

            ## Computing larger face co-ordinates
            X_1, X_2 = (max(0, X - int(w * 0.35)), min(X + int(1.35 * w), W))
            Y_1, Y_2 = (max(0, Y - int(0.35 * h)), min(Y + int(1.35 * h), H))

            ## Cropping face and changing BGR To RGB
            img_cp = frame[Y_1:Y_2, X_1:X_2].copy()
            img_cp1 = cv2.cvtColor(img_cp, cv2.COLOR_BGR2RGB)

            ## Prediction of facial featues
            prediction = str(
                learn.predict(Image(pil2tensor(
                    img_cp1, np.float32).div_(255)))[0]).split(";")
            label = (" ".join(prediction) if "Male" in prediction else
                     "Female " + " ".join(prediction))
            label = (" ".join(prediction) if "No_Beard" in prediction else
                     "Beard " + " ".join(prediction))

            ## Drawing facial boundaries
            cv2.rectangle(
                img=frame,
                pt1=(X, Y),
                pt2=(X + w, Y + h),
                color=(128, 128, 0),
                thickness=2,
            )

            ## Drawing facial attributes identified
            label_list = label.split(" ")
            for idx in range(1, len(label_list) + 1):
                cv2.putText(
                    frame,
                    label_list[idx - 1],
                    (X, Y - 14 * idx),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.45,
                    (0, 128, 0),
                    2,
                )

        # Display the resulting frame
        cv2.imshow("frame", frame)

        ## Save the resulting frame
        if save_video:
            out.write(frame)

        ## Escape keys
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    # When everything done, release the capture
    cap.release()
    if save_video:
        out.release()
    cv2.destroyAllWindows()