Esempio n. 1
0
    def to(self, *args: Any, **kwargs: Any) -> 'AnalogContext':
        """Move analog tiles of the current context to a device.

        Note:
            Please be aware that moving analog tiles from GPU to CPU is
            currently not supported.

        Caution:
            Other tensor conversions than moving the device to CUDA,
            such as changing the data type are not supported for analog
            tiles and will be simply ignored.

        Returns:
            This module in the specified device.
        """
        # pylint: disable=invalid-name, attribute-defined-outside-init
        self.data = self.data.to(*args, **kwargs)

        device = None
        if 'device' in kwargs:
            device = kwargs['device']
        elif len(args) > 0 and not isinstance(args[0], (Tensor, dtype)):
            device = torch_device(args[0])

        if device is not None:
            device = torch_device(device)
            if device.type == 'cuda' and not self.analog_tile.is_cuda:
                self.analog_tile = self.analog_tile.cuda(device)
            self.reset(self.analog_tile)

        return self
Esempio n. 2
0
 def __init__(
     self,
     dataset: Dataset,
     instance: Instance,
     model: Model,
     optimizer: Optimizer,
     scheduler: Any,
     epochs: int,
     batch_size: int,
     run_dir_path: Path,
     eval_every: int,
     limit_epochs_at: Optional[int],
     train_eval_split: float,
     metric_names: List[str],
     selection_metric: str,
     kept_checkpoints: int,
     cuda_device: Optional[int],
 ) -> None:
     self.dataset = dataset
     self.instance = instance
     self.optimizer = optimizer
     self.scheduler = scheduler
     self.epochs = epochs
     self.batch_size = batch_size
     self.run_dir_path = run_dir_path
     self.eval_every = eval_every
     self.train_eval_split = train_eval_split
     self.limit_epochs_at = 10e1000 if limit_epochs_at is None else limit_epochs_at
     self.selection_metric = selection_metric
     self.kept_checkpoints = kept_checkpoints
     self._checkpoints_stats: List[Tuple[float, str]] = []
     if cuda_device is not None:
         if not cuda_is_available():
             raise RuntimeError("CUDA is not available on this system.")
         self.use_cuda = True
         self.device = torch_device("cuda:%d" % cuda_device)
     else:
         self.use_cuda = False
         self.device = torch_device("cpu")
     self.model = model.to(self.device)
     self._checkpoints_dir = self.run_dir_path / "checkpoints"
     self._writers: Dict[DataType, SummaryWriter] = {}
     self._accumulated_metrics: Dict[DataType, Dict[
         Metric,
         List[float]]] = defaultdict(lambda: defaultdict(lambda: []))
     self._metrics = [_metrics[metric_name] for metric_name in metric_names]
     self._metric_names = {
         metric: metric_name
         for metric, metric_name in zip(self._metrics, metric_names)
     }
     self._epochs_size = len(str(epochs))
Esempio n. 3
0
    def __init__(
            self,
            out_size: int,
            in_size: int,
            rpu_config: RPUConfigGeneric,
            bias: bool = True,
            in_trans: bool = False,
            out_trans: bool = False
    ):
        self.out_size = out_size
        self.in_size = in_size
        self.rpu_config = rpu_config
        self.bias = bias
        self.in_trans = in_trans
        self.out_trans = out_trans

        # Only used for indexed.
        self.image_sizes = []  # type: List[int]

        x_size = in_size + 1 if self.bias else in_size
        d_size = out_size

        self.tile = self._create_simulator_tile(x_size, d_size, rpu_config)
        self.tile.set_learning_rate(0.01)
        self.tile.set_weights_uniform_random(-0.01, 0.01)

        self.device = torch_device('cpu')
        self.is_cuda = False
        self.shared_weights = None  # type: Optional[Tensor]

        # create analog context
        self.analog_ctx = AnalogContext(self)
Esempio n. 4
0
    def cuda(
            self,
            device: Optional[Union[torch_device, str,
                                   int]] = None) -> 'BaseTile':
        """Return a copy of this tile in CUDA memory.

        Args:
            device: CUDA device

        Returns:
            Self with the underlying C++ tile moved to CUDA memory.

        Raises:
            CudaError: if the library has not been compiled with CUDA.
        """
        if not cuda.is_compiled():
            raise CudaError('aihwkit has not been compiled with CUDA support')

        device = torch_device('cuda', cuda_device(device).idx)

        if self.is_cuda and device != self.device:
            raise CudaError(
                'Cannot switch CUDA devices of existing Cuda tiles')

        if isinstance(self.tile, tiles.AnalogTile):
            with cuda_device(device):
                self.tile = tiles.CudaAnalogTile(self.tile)
                self.is_cuda = True
                self.device = device
                self.analog_ctx.cuda(device)

        return self
Esempio n. 5
0
    def __init__(self,
                 model,
                 trainset_cls,
                 testset_cls=None,
                 test_apply=None,
                 batch_size=8,
                 lr=0.01,
                 clip_grad=0,
                 weight_decay=0,
                 optimizer='sgd',
                 criterion=nn.CrossEntropyLoss,
                 device='cuda',
                 dataset_caching=True):

        assert isinstance(model, nn.Module)

        self.trainset_cls = trainset_cls
        self.testset_cls = testset_cls or trainset_cls
        self.test_apply = test_apply

        self.batch_size = batch_size
        self.clip_grad = clip_grad
        self.device = torch_device(device)

        self.model = model
        self.model.to(device)
        parameters = filter(lambda p: p.requires_grad, self.model.parameters())
        self.optimizer = OPTIMIZERS[optimizer](parameters,
                                               lr=lr,
                                               weight_decay=weight_decay)
        self.criterion = criterion()

        self._dataset_caching = dataset_caching
        self._dataset_cache = {}
        self._dataset_hash = {}
Esempio n. 6
0
    def to(
        self,
        device: Optional[Union[torch_device, str, int]] = None
    ) -> 'AnalogSequential':
        """Move and/or cast the parameters, buffers and analog tiles.

        Note:
            Please be aware that moving analog layers from GPU to CPU is
            currently not supported.

        Args:
            device: the desired device of the parameters, buffers and analog
                tiles in this module.

        Returns:
            This module in the specified device.
        """
        # pylint: disable=arguments-differ
        device = torch_device(device)

        super().to(device)

        if device.type == 'cuda':
            self._apply_to_analog(lambda m: m.cuda(device))
        elif device.type == 'cpu':
            self._apply_to_analog(lambda m: m.cpu())

        return self
Esempio n. 7
0
    def __init__(self,
                 out_size: int,
                 in_size: int,
                 rpu_config: RPUConfigGeneric,
                 bias: bool = True,
                 in_trans: bool = False,
                 out_trans: bool = False):
        self.out_size = out_size
        self.in_size = in_size
        self.rpu_config = rpu_config
        self.bias = bias
        self.in_trans = in_trans
        self.out_trans = out_trans

        # Only used for indexed.
        self.image_sizes = []  # type: List[int]

        x_size = in_size + 1 if self.bias else in_size
        d_size = out_size

        # Cuda tiles are assumed to init `self.tile` manually.
        if self.is_cuda:
            self.tile = None  # type: Union[tiles.FloatingPointTile, tiles.AnalogTile]
        else:
            self.tile = self._create_simulator_tile(x_size, d_size, rpu_config)
            self.tile.set_learning_rate(0.01)
            self.tile.set_weights_uniform_random(-0.01, 0.01)

        self.device = torch_device('cpu')
Esempio n. 8
0
def generate_images(model,
                    batch,
                    mask_descriptors,
                    num_samples=64,
                    temp=1.,
                    verbose=False):
    """Generates image completions based on the images in batch masked by the
    masks in mask_descriptors. This will generate
    batch.size(0) * len(mask_descriptors) * num_samples completions, i.e.
    num_samples completions for every image and mask combination.

    Parameters
    ----------
    model : pixconcnn.models.pixel_constrained.PixelConstrained instance

    batch : torch.Tensor

    mask_descriptors : list of mask_descriptor
        See utils.masks.MaskGenerator for allowed mask_descriptors.

    num_samples : int
        Number of samples to generate for a given image-mask combination.

    temp : float
        Temperature for sampling.

    verbose : bool
        If True prints progress information while generating images
    """
    device = torch_device("cuda" if cuda_is_available() else "cpu")
    model.to(device)
    outputs = []
    for i in range(batch.size(0)):
        outputs_per_img = []
        for j in range(len(mask_descriptors)):
            if verbose:
                print("Generating samples for image {} using mask {}".format(
                    i, mask_descriptors[j]))
            # Get image and mask combination
            img = batch[i:i + 1]
            mask_generator = MaskGenerator(model.prior_net.img_size,
                                           mask_descriptors[j])
            mask = mask_generator.get_masks(1)
            # Create conditional pixels which will be used to sample completions
            cond_pixels = get_repeated_conditional_pixels(
                img, mask, model.prior_net.num_colors, num_samples)
            cond_pixels = cond_pixels.to(device)
            samples, log_probs = model.sample(cond_pixels,
                                              return_likelihood=True,
                                              temp=temp)
            outputs_per_img.append({
                "orig_img": img,
                "cond_pixels": cond_pixels,
                "mask": mask,
                "samples": samples,
                "log_probs": log_probs
            })
        outputs.append(outputs_per_img)
    return outputs
Esempio n. 9
0
 def test_enum_sobol_GPEI(self):
     """Tests Soboland GPEI instantiation through the Models enum."""
     exp = get_branin_experiment()
     # Check that factory generates a valid sobol modelbridge.
     sobol = Models.SOBOL(search_space=exp.search_space)
     self.assertIsInstance(sobol, RandomModelBridge)
     for _ in range(5):
         sobol_run = sobol.gen(n=1)
         self.assertEqual(sobol_run._model_key, "Sobol")
         exp.new_batch_trial().add_generator_run(sobol_run).run()
     # Check that factory generates a valid GP+EI modelbridge.
     exp.optimization_config = get_branin_optimization_config()
     gpei = Models.GPEI(experiment=exp, data=exp.fetch_data())
     self.assertIsInstance(gpei, TorchModelBridge)
     self.assertEqual(gpei._model_key, "GPEI")
     botorch_defaults = "ax.models.torch.botorch_defaults"
     # Check that the callable kwargs and the torch kwargs were recorded.
     self.assertEqual(
         gpei._model_kwargs,
         {
             "acqf_constructor": {
                 "is_callable_as_path": True,
                 "value": f"{botorch_defaults}.get_NEI",
             },
             "acqf_optimizer": {
                 "is_callable_as_path": True,
                 "value": f"{botorch_defaults}.scipy_optimizer",
             },
             "model_constructor": {
                 "is_callable_as_path": True,
                 "value": f"{botorch_defaults}.get_and_fit_model",
             },
             "model_predictor": {
                 "is_callable_as_path": True,
                 "value": f"{botorch_defaults}.predict_from_model",
             },
             "refit_on_cv": False,
             "refit_on_update": True,
             "warm_start_refitting": True,
         },
     )
     self.assertEqual(
         gpei._bridge_kwargs,
         {
             "transform_configs": None,
             "torch_dtype": torch_float64,
             "torch_device": torch_device(type="cpu"),
             "status_quo_name": None,
             "status_quo_features": None,
             "optimization_config": None,
             "transforms": Cont_X_trans + Y_trans,
         },
     )
     gpei = Models.GPEI(experiment=exp,
                        data=exp.fetch_data(),
                        search_space=exp.search_space)
     self.assertIsInstance(gpei, TorchModelBridge)
Esempio n. 10
0
def compute_pulse_response(analog_tile: BaseTile,
                           direction: ndarray,
                           use_forward: bool = False) -> ndarray:
    """Computes the pulse response of a given device configuration.

    Args:
        analog_tile: Base tile used for computing the weight traces
        direction: numpy vector of directions to sequentially apply (-1 or 1)
        use_forward: Whether to use the (noisy) forward pass to read out the weights
            (otherwise returns exact weight value).

    Returns:
        An numpy array ``w_trace`` of dimensions ``len(direction) x out_size x in_size``
    """
    out_size = analog_tile.out_size
    in_size = analog_tile.in_size

    if analog_tile.is_cuda:
        device = torch_device('cuda')
    else:
        device = torch_device('cpu')

    total_iters = len(direction)
    w_trace = np.zeros((total_iters, out_size, in_size))
    in_vector = -ones(1, in_size, device=device)
    out_vector = ones(1, out_size, device=device)
    in_eye = eye(in_size, device=device)
    dir_tensor = from_numpy(direction).float().to(device)

    for i in range(total_iters):
        # Update the pulses.
        analog_tile.update(in_vector, out_vector * dir_tensor[i])

        if use_forward:
            # Save weights by using the forward pass (to get the short-term read noise).
            w_trace[i, :, :] = analog_tile.forward(
                in_eye).detach().cpu().numpy().T
        else:
            # Noise free.
            w_trace[
                i, :, :] = analog_tile.get_weights()[0].detach().cpu().numpy()

    return w_trace
Esempio n. 11
0
def make_sequence_video(cfg):
    with open(cfg) as fd:
        data_specs = json.load(fd)

    temp_size = data_specs['temp_size']
    num_of_temp_features = data_specs['temp_features']
    m = load_model(data_specs['model_path'],
                   DOTNetCNN.name(),
                   # ToNameNet.name(),
                   "model_params.json")
    use_gpu = torch_cuda.is_available()
    device = torch_device(torch_cuda.current_device()) if use_gpu else torch_device("cpu")
    m.to(device)

    r = get_images_classes(
        data_specs['images_path'],
        data_specs['info_dict'],
        data_specs['class_of_interest']
    )
    train_d, val_d, test_d = split_data(
        data_specs['positive_ev_path'],
        r,
        window_size=temp_size,
        future_size=0,
        shuffle=False
    )
    full_df = pd.concat([train_d, val_d, test_d], ignore_index=True)

    info_path = data_specs["info_path"]
    ds = PixelLevelDs(
        full_df,
        info_path=info_path,
        add_polarity="neg" if num_of_temp_features > 1 else "",
    )
    # make_video(m, ds)
    make_non_repeating_video(m, ds)
Esempio n. 12
0
    def __init__(self, source_tile: FloatingPointTile):
        if not cuda.is_compiled():
            raise CudaError('aihwkit has not been compiled with CUDA support')

        # Create a new instance of the rpu config.
        new_rpu_config = deepcopy(source_tile.rpu_config)

        # Create the tile, replacing the simulator tile.
        super().__init__(source_tile.out_size, source_tile.in_size, new_rpu_config,
                         source_tile.bias, source_tile.in_trans, source_tile.out_trans)
        self.tile = tiles.CudaFloatingPointTile(source_tile.tile)

        # Set the cuda properties
        self.stream = current_stream()
        self.device = torch_device(current_device())
    def test_run_example_gpu(self):
        """Test running the example using a local runner."""
        training_experiment = self.get_experiment()
        local_runner = LocalRunner(device=torch_device('cuda'))

        with patch('sys.stdout', new=StringIO()) as captured_stdout:
            result = local_runner.run(training_experiment, max_elements_train=10)

        # Asserts over stdout.
        self.assertIn('Epoch: ', captured_stdout.getvalue())

        # Asserts over the returned results.
        self.assertEqual(len(result), 1)
        self.assertEqual(result[0]['epoch'], 0)
        self.assertIn('train_loss', result[0])
        self.assertIn('accuracy', result[0])
Esempio n. 14
0
    def __init__(self,
                 out_size: int,
                 in_size: int,
                 resistive_device: Optional[BaseResistiveDevice] = None,
                 bias: bool = False,
                 in_trans: bool = False,
                 out_trans: bool = False):
        if not cuda.is_compiled():
            raise RuntimeError(
                'aihwkit has not been compiled with CUDA support')
        super().__init__(out_size, in_size, resistive_device, bias, in_trans,
                         out_trans)

        self.tile = tiles.CudaAnalogTile(self.tile)
        self.stream = current_stream()
        self.device = torch_device(current_device())
Esempio n. 15
0
    def __init__(self, source_tile: AnalogTile):
        if not cuda.is_compiled():
            raise RuntimeError(
                'aihwkit has not been compiled with CUDA support')

        # Create a new instance of the resistive device.
        new_resistive_device = deepcopy(source_tile.resistive_device)

        # Create the tile, replacing the simulator tile.
        super().__init__(source_tile.out_size, source_tile.in_size,
                         new_resistive_device, source_tile.bias,
                         source_tile.in_trans, source_tile.out_trans)
        self.tile = tiles.CudaAnalogTile(source_tile.tile)

        # Set the cuda properties
        self.stream = current_stream()
        self.device = torch_device(current_device())
Esempio n. 16
0
    def __init__(self, path):
        self.config_path = os.path.join(path, 'config.json')
        self.model_path = os.path.join(path, 'model_best.pth')

        config = json.load(open(self.config_path))
        checkpoint = torch_load(self.model_path, map_location='cpu')
        m_name, sd, self.classes = _get_model_att(checkpoint)

        model = vgg11_bn(self.classes, pretrained=False)
        model.load_state_dict(checkpoint['state_dict'])

        tsf = _get_transform(config)

        # self.device = torch_device("cuda" if torch_cuda.is_available() else "cpu")
        self.device = torch_device("cpu")
        self.model = model.to(self.device)
        self.model.eval()
        self.transforms = tsf
Esempio n. 17
0
def get_answer(sentence):
    with open(
            os.path.join(os.path.dirname(__file__), '..', 'data',
                         'intents.json'), 'r') as f:
        intents = json.load(f)

    FILE = os.path.join(os.path.dirname(__file__), '..', 'data', 'data.pth')
    data = torch_load(FILE)

    input_size = data['input_size']
    hidden_size = data['hidden_size']
    output_size = data['output_size']
    all_words = data['all_words']
    tags = data['tags']
    model_state = data['model_state']

    device = torch_device('cuda' if torch_cuda.is_available() else 'cpu')
    model = NeuralNet(input_size, hidden_size, output_size).to(device)
    model.load_state_dict(model_state)
    model.eval()

    sentence = tokenize(sentence)
    X = bag_of_words(sentence, all_words)
    X = X.reshape(1, X.shape[0])
    X = torch_from_numpy(X)

    output = model(X)
    _, predicted = torch_max(output, dim=1)
    tag = tags[predicted.item()]

    answer = ''
    probs = torch_softmax(output, dim=1)
    prob = probs[0][predicted.item()]
    if prob.item() > 0.75:
        for intent in intents['intents']:
            if tag == intent['tag']:
                answer = random.choice(intent['responses'])
    else:
        answer = "I don't understand..."

    return answer, prob
Esempio n. 18
0
def predict_dataset(dataset, model, device, test_apply=None, batch_size=4):
    loader = DataLoader(dataset,
                        batch_size=batch_size,
                        shuffle=False,
                        num_workers=0,
                        pin_memory=cuda.is_available())
    y_pred = []

    with test_mode(model):
        for x in loader:
            x = x.to(device)

            if test_apply is not None:
                out = test_apply(x, model)
            else:
                out = model(x)

            y_pred.append(out.to(torch_device('cpu')).numpy())

    y_pred = np.vstack(y_pred)
    return y_pred
Esempio n. 19
0
    def __init__(self,
                 out_size: int,
                 in_size: int,
                 resistive_device: BaseResistiveDevice,
                 bias: bool = True,
                 in_trans: bool = False,
                 out_trans: bool = False,
                 _from_tile: Optional[Any] = None):
        self.out_size = out_size
        self.in_size = in_size
        self.resistive_device = resistive_device
        self.bias = bias
        self.in_trans = in_trans
        self.out_trans = out_trans

        x_size = in_size + 1 if self.bias else in_size
        d_size = out_size

        if _from_tile is not None:
            self.tile = _from_tile
        else:
            self.tile = self.resistive_device.create_tile(x_size, d_size)
        self.device = torch_device('cpu')
Esempio n. 20
0
    def __init__(self,
                 out_size: int,
                 in_size: int,
                 resistive_device: BaseResistiveDevice,
                 bias: bool = True,
                 in_trans: bool = False,
                 out_trans: bool = False):
        self.out_size = out_size
        self.in_size = in_size
        self.resistive_device = resistive_device
        self.bias = bias
        self.in_trans = in_trans
        self.out_trans = out_trans

        x_size = in_size + 1 if self.bias else in_size
        d_size = out_size

        # Cuda tiles are assumed to init `self.tile` manually.
        if self.is_cuda:
            self.tile = None  # type: Union[tiles.FloatingPointTile, tiles.AnalogTile]
        else:
            self.tile = self.resistive_device.create_tile(x_size, d_size)

        self.device = torch_device('cpu')
Esempio n. 21
0
class GazeDetector:
    """
    Detect a user's gaze to see if they are engaged
    or not
    """
    weights_path: str = os.path.abspath("machine_learning/models/weights/gazenet.pth")
    coordinate_length: float = 200

    device: torch_device = torch_device("cuda:0" if torch_gpu.is_available() else "cpu")
    model: gaze_cnn.GazeNet = gaze_cnn.GazeNet(device=device)
    model.load_state_dict(torch_load(weights_path, map_location=device))
    model.eval()

    def __init__(self, logger, base_64_image):
        """
        Base 64 image from the student app
        :param base_64_image: string from client in request
        """
        self.logger = logger
        self.opencv_image = self._read_base64(base_64_image)
        self.face_detector = FaceDetector(device=self.device)

    @staticmethod
    def _read_base64(base64_string: str) -> cvtColor:
        """
        Read base64 string into CV2
        :param base64_string: base 64 string
        :return: cv2 image
        """
        byte_buffer: BytesIO = BytesIO()
        byte_buffer.write(base64_decode(base64_string))
        pil_img: Image = Image.open(byte_buffer)
        return cvtColor(np.array(pil_img), COLOR_RGB2BGR)

    @staticmethod
    def _normalize_face(landmarks, frame) -> tuple:
        """
        Normalize the face to a standard map
        :param landmarks: landmarks on the face
        :param frame: image frame
        :return: the face, gaze origin and
        """
        left_eye_coord: tuple = (0.70, 0.35)
        lcenter: tuple = tuple([landmarks[0], landmarks[5]])
        rcenter: tuple = tuple([landmarks[1], landmarks[6]])

        gaze_origin: tuple = (int((lcenter[0] + rcenter[0]) / 2), int((lcenter[1] + rcenter[1]) / 2))

        dY: float = rcenter[1] - lcenter[1]
        dX: float = rcenter[0] - lcenter[0]
        angle = np.degrees(np.arctan2(dY, dX)) - 180
        right_eye_x = 1.0 - left_eye_coord[0]
        dist: np.float32 = np.sqrt((dX ** 2) + (dY ** 2))
        new_dist = (right_eye_x - left_eye_coord[0])
        new_dist *= 112
        scale: np.float32 = new_dist / dist
        M = getRotationMatrix2D(gaze_origin, angle, scale)

        tX = 112 * 0.5
        tY = 112 * left_eye_coord[1]
        M[0, 2] += (tX - gaze_origin[0])
        M[1, 2] += (tY - gaze_origin[1])

        face = warpAffine(frame, M, (112, 112), flags=INTER_CUBIC)
        return face, gaze_origin, M

    @staticmethod
    def _get_vector(pitch_yaw: List) -> (np.float32, np.float32):
        """
        Get the deltas in the x and y direction to measure gaze
        :param pitch_yaw: gaze coords from CNN
        :return: dx, dy
        """
        dx: np.float32 = -GazeDetector.coordinate_length * np.sin(pitch_yaw[1])
        dy: np.float32 = -GazeDetector.coordinate_length * np.sin(pitch_yaw[0])  # coords start in upper left corner
        return dx, dy

    @staticmethod
    def _compute_angle(dx: float, dy: float) -> np.float32:
        """
        Computes the angle in degrees of the vector defined
        by dx and dy

        :param dx: delta x for vector
        :param dy: delta y for vector
        :return: angle between standard position and the vector in degs
        """
        return np.arctan2(dy, dx) * (180 / np.pi)  # convert to degrees from radians

    @staticmethod
    def _compute_magnitude(dx: float, dy: float) -> float:
        """
        Compute the magnitude of vector <dy, dx>
        :param dx: delta x for vector
        :param dy: delta y for vector
        :return: magnitude of vector <dx, dy>
        """
        return np.sqrt(dx ** 2 + dy ** 2)  # find magnitude of vector

    def predict_gaze(self) -> (float, float):
        """
        Predict the Gaze trajectory as a vector using our GazeNet CNN
        and return the angle and magnitude of the vector
        :return: angle between delta x and delta y
        """
        self.opencv_image = flip_image(self.opencv_image[:, :, ::-1], 1)  # helps to rectify rectangular coordinates
        faces, landmarks = self.face_detector.detect(Image.fromarray(self.opencv_image))

        if len(faces) == 0:
            self.logger.info("No faces found!")
            return 0, 0, "NOT ENGAGED", 0

        for face, landmark in zip(faces, landmarks):
            if face[-1] > 0.98:  # confidence check
                normal_face, gaze_origin, _ = GazeDetector._normalize_face(landmark, self.opencv_image)
                # Predict Gaze
                with torch_no_grad():
                    gaze = GazeDetector.model.get_gaze(normal_face)
                    gaze = gaze[0].data.cpu()
                #self.logger("Predicted on CNN: Gaze com puted to be " + str(gaze))
                dx, dy = self._get_vector(gaze)
                dy *= -1
                #self.logger.info("Original Gaze Location: " + str(gaze_origin))
                #self.logger.info("Delta X: " + str(dx) + "; Delta Y: " + str(dy))
                
                computed_angle = self._compute_angle(dx, dy).cpu().numpy()
                computed_magnitude = self._compute_magnitude(dx, dy).cpu().numpy()
                classification, score = self.interpret_vectors(computed_angle, computed_magnitude)

                #self.logger.info("Trying to save gaze computation")
                # showComputedGaze(Image.fromarray(self.opencv_image), gaze_origin, gaze, computed_angle, computed_magnitude, score, classification)
                return computed_angle, computed_magnitude, classification, score
            else:
                return 0, 0, "NOT ENGAGED", 0
                

    def interpret_vectors(self, angle: np.float32, magnitude: np.float32):
        """
        Interpret the Vector's Position and Magnitude for engagement/disengagement
        :param angle: the angle of the gaze vector
        :param magnitude: the magnitude of the gaze vector
        :return: student state and confidence
        """
        Rule = namedtuple('Rule', ['trigger_str', 'confidence', 'result', 'score'])

        rules: List[Rule] = [  # in order of precedence
            Rule('angle == -1.0 and magnitude == -1.0', 0.0, "NOT ENGAGED", 0),
            Rule('magnitude <= 30.0 or 0 < angle <= 15', 0.95, "ENGAGED", 10),
            Rule('magnitude <= 40.0', 0.95, "ENGAGED", 9),
            Rule('magnitude <= 50.0', 0.60, "ENGAGED",8),
            Rule('magnitude <= 60.0', 0.65, "ENGAGED", 7),
            Rule('15 < angle < 50', 0.70, "SOMEWHAT ENGAGED", 6),
            Rule('60 < magnitude <= 70', 0.65, "SOMEWHAT ENGAGED", 5),
            Rule('70 < magnitude <= 120', 0.70, "NOT ENGAGED", 4),
            Rule('120 < magnitude < 360', 0.70, "NOT ENGAGED", 3),
            Rule('magnitude > 60.0', 0.70, "NOT ENGAGED", 2),
            Rule('magnitude > 50.0', 0.85, "NOT ENGAGED", 1),
            Rule('True', 0.0, "ENGAGED", 10)  # default condition (base case-- should never be called)
        ]

        for rule_no, rule in enumerate(rules):
            if eval(rule.trigger_str):
                self.logger.info("Triggered rule: " + rule.trigger_str + ": " + str(rule))
                return rule.result, rule.score
Esempio n. 22
0
def process_each_frequency(model_dirname, stft, frequency, using_cuda=True):
    '''
    Setter method on stft.
    '''
    is_using_cuda = using_cuda and torch_cuda_is_cuda_available()
    my_device = torch_device('cuda:0' if is_using_cuda else 'cpu')

    # 1. Instantiate Neural Network Model
    model_params_fname = os_path_join(
        os_path_join(model_dirname, 'k_' + str(frequency)), MODEL_PARAMS_FNAME)

    model_save_fpath = os_path_join(model_dirname, 'k_' + str(frequency),
                                    MODEL_SAVE_FNAME)
    model = get_which_model_from_params_fname(model_params_fname)
    model.load_state_dict(torch_load(os_path_join(
        os_path_dirname(model_save_fpath), 'model.dat'),
                                     map_location=my_device),
                          strict=True)
    model.eval()
    model = model.to(my_device)

    if False:
        model.printing = True
        from lib.print_layer import PrintLayer
        new_model_net = []
        for layer in model.net:
            new_model_net.append(layer)
            new_model_net.append(PrintLayer(layer))

        from torch.nn import Sequential
        model.net = Sequential(*new_model_net)
    # 2. Get X_test
    LOGGER.debug('r3.process_each_frequency: stft.shape = {}'.format(
        stft.shape))

    aperture_data = stft[:, :, frequency]  # or stft_frequency

    # 2.1. normalize by L1 norm
    aperture_data_norm = np_linalg_norm(aperture_data, ord=np_inf, axis=1)
    aperture_data /= aperture_data_norm[:, np_newaxis]

    # load into torch and onto gpu
    aperture_dataset_eval = ApertureDatasetEval(aperture_data)
    aperture_dataset_loader = DataLoader(aperture_dataset_eval,
                                         batch_size=EVAL_BATCH_SIZE,
                                         shuffle=False,
                                         num_workers=DATALOADER_NUM_WORKERS,
                                         pin_memory=using_cuda)

    # 3. Predict
    if is_using_cuda is True:
        torch_cuda_empty_cache()

    aperture_data_new = predict(model, aperture_dataset_loader, my_device)

    del aperture_data, model, aperture_dataset_eval, aperture_dataset_loader, my_device
    if is_using_cuda is True:
        torch_cuda_empty_cache()
    # 4. Postprocess on y_hat
    # rescale the data and store new data in stft
    stft[:, :,
         frequency] = aperture_data_new * aperture_data_norm[:, np_newaxis]
    del aperture_data_new, aperture_data_norm
Esempio n. 23
0
        if config.color:
            config.tau_size = 3
        else:
            config.tau_size = 2
        config.eof_size = 15
        config.aux_size = 2
        config.out_size = 7
    else:
        config.eof_size = 15
        config.tau_size = 3
        config.aux_size = 6
        config.out_size = 6


    if use_cuda():
        config.device = torch_device(args.device)
    else:
        config.device = torch_device('cpu')

    # Compilation params
    config.dest_dir     = config.data_file
    config.test_cases   = ()
    config.split_percen = .99
    config.simulation   = config.sim
    config.max_traj     = args.num_traj

    # Data gathering params
    config.normalize   = False
    config.num_traj    = args.num_traj
    config.framerate   = args.framerate
    config.save_folder = config.root_dir
Esempio n. 24
0
def train(
    *,
    instance_file: str,
    tensors_dir: str,
    train_dir: str,
    configs_dir: str,
    model_encoder_iterations: int,
    model_encoder_output_dim: int,
    model_encoder_message_dim: int,
    model_decoder_type: str,
    model_learning_rate: float,
    model_batch_size: int,
    trainer_epochs: int,
    trainer_eval_every: int,
    trainer_limit_epochs_at: Optional[int],
    trainer_train_eval_split: float,
    trainer_selection_metric: str,
    trainer_kept_checkpoints: int,
    trainer_cuda: Optional[int],
    log_level: str,
) -> None:
    """Run the training."""
    Config.from_arguments(locals(),
                          ["instance_file", "tensors_dir", "train_dir"],
                          "configs_dir").save(
                              Path(configs_dir) / "train.json")
    logger = setup_logging(__name__, log_level)

    tensors_dir_path = Path(tensors_dir).expanduser().resolve()
    train_dir_path = Path(train_dir).expanduser().resolve()
    train_dir_path.mkdir(parents=True, exist_ok=True)

    with bz2_open(instance_file, "rb") as fh:
        instance = pickle_load(fh)

    dataset = CodRepDataset(input_dir=tensors_dir_path)
    logger.info("Dataset of size %d", len(dataset))

    train_length = round(0.9 * len(dataset))
    eval_length = round(0.05 * len(dataset))
    test_length = len(dataset) - train_length - eval_length

    train_dataset, eval_dataset, test_dataset = random_split(
        dataset, [train_length, eval_length, test_length])

    if trainer_cuda is not None:
        if not cuda_is_available():
            raise RuntimeError("CUDA is not available on this system.")
        device = torch_device("cuda:%d" % trainer_cuda)
    else:
        device = torch_device("cpu")
    model = build_model(
        instance=instance,
        model_encoder_iterations=model_encoder_iterations,
        model_encoder_output_dim=model_encoder_output_dim,
        model_encoder_message_dim=model_encoder_message_dim,
        model_decoder_type=model_decoder_type,
        model_learning_rate=model_learning_rate,
        model_batch_size=model_batch_size,
        train_dataset=train_dataset,
        eval_dataset=eval_dataset,
        test_dataset=test_dataset,
    )
    # The model needs a forward to be completely initialized.
    model.training_step(instance.collate([dataset[0]]), 0)
    logger.info("Configured model %s", model)

    checkpoint_callback = ModelCheckpoint(
        filepath=train_dir,
        save_best_only=True,
        verbose=True,
        monitor="eval_mrr",
        mode="max",
        prefix="",
    )

    trainer = Trainer(default_save_path=train_dir,
                      checkpoint_callback=checkpoint_callback)
    trainer.fit(model)
Esempio n. 25
0
# this dictionary contains the basic parameters for training the model
custom_config = dict(dim=1000,
                     margin=10,
                     batch_size=1000,
                     learning_rate=0.2,
                     num_epochs=200,
                     corruption_factor=5,
                     filter_validation_triples=False,
                     early_stopping=True,
                     device="cuda:1")
config = BASE_CONFIG.copy()
config.update(custom_config)
config['model'] = list(MODELS)[model_type_train]

if cuda.is_available() and config['device'] != "cpu":
    device = torch_device(config['device'])
else:
    config['device'] = "cpu"
    device = torch_device("cpu")
print("Using {}".format(config['device']))

if SEED:
    manual_seed(0)  # pytorch seed
    seed(0)  # numpy seed

if TRAIN_MODEL:
    start = time.time()
    run_train(config=config,
              device=device,
              dataset=dataset,
              align_dataset=align_data_train,
Esempio n. 26
0
N_si = 3.48  # silicon
eps_si = N_si**2

N_sio2 = 1.44  # silicon oxide
eps_sio2 = N_sio2**2

N_sinitride = 1.99  # silicon nitride
eps_sinitride = N_sinitride**2

# Physical constants ===================================================================================================
OMEGA_1550 = 1.215e15  # 1550nm frequency
MU0 = 4 * pi * 10**-7  # vacuum permeability
EPSILON0 = 8.854187817620e-12  # vacuum permittivity
C = 299792458.0  # speed of light
BUFFER_PERMITTIVITY = -1e20  # near infinite permittivity for cavity boundaries
# BUFFER_PERMITTIVITY = 1.0  # vacuum permittivity if using PML

# Design space size ====================================================================================================
DEVICE_LENGTH = 64  # length of permittivity region
DEVICE_LENGTH_2D = 32
NPML = 0  # number of PMLs
BUFFER_LENGTH = 4  # buffer size before NPML (reflective boundary if using cavity)
SCALE = 1e-15
L0 = 1e-6
dL = 0.05
PIXEL_SIZE = dL * L0

# Device configuration =================================================================================================
device = torch_device('cuda:0')
# device = torch_device('cpu')
Esempio n. 27
0
    def __setstate__(self, state: Dict) -> None:
        """Set the state after unpickling.

        This method recreates the ``tile`` member, creating a new one from
        scratch, as the binding Tiles are not serializable.

        Caution:
            RPU configs are overwritten by loading the state.

        Raises:
            TileError: if tile class does not match or hidden parameters do not match
        """
        # pylint: disable=too-many-locals

        # Note: self here is NOT initialized! So we need to recreate
        # attributes that were not saved in getstate

        current_dict = state.copy()
        weights = current_dict.pop('analog_tile_weights')
        hidden_parameters = current_dict.pop('analog_tile_hidden_parameters')
        hidden_parameters_names = current_dict.pop(
            'analog_tile_hidden_parameter_names', [])
        alpha_scale = current_dict.pop('analog_alpha_scale', None)
        tile_class = current_dict.pop('analog_tile_class',
                                      self.__class__.__name__)
        analog_lr = current_dict.pop('analog_lr', 0.01)
        analog_ctx = current_dict.pop('analog_ctx')
        shared_weights = current_dict.pop('shared_weights')
        shared_weights_if = shared_weights is not None

        self.__dict__.update(current_dict)

        self.device = torch_device('cpu')
        self.is_cuda = False
        # get the current map location from analog_ctx (which is restored)
        to_device = analog_ctx.device

        # recreate attributes not saved
        # always first create on CPU
        x_size = self.in_size + 1 if self.bias else self.in_size
        d_size = self.out_size

        # Recreate the tile.
        # Check for tile mismatch
        if tile_class != self.__class__.__name__:
            raise TileError(
                'Mismatch of tile class: {} versus {}. Can only load analog '
                'state from the same tile class.'.format(
                    self.__class__.__name__, tile_class))

        self.tile = self._create_simulator_tile(x_size, d_size,
                                                self.rpu_config)
        names = self.tile.get_hidden_parameter_names()
        if len(hidden_parameters_names
               ) > 0 and names != hidden_parameters_names:
            # Check whether names match
            raise TileError('Mismatch with loaded analog state: '
                            'Hidden parameter structure is unexpected.')
        self.tile.set_hidden_parameters(Tensor(hidden_parameters))
        self.tile.set_weights(weights)

        self.tile.set_learning_rate(analog_lr)

        # re-generate shared weights (CPU)
        if shared_weights_if:
            if not hasattr(self, 'shared_weights'):
                # this is needed when pkl loading
                self.shared_weights = shared_weights

            with no_grad():
                # always new will be populated with set weights.
                self.shared_weights.data = zeros(d_size,
                                                 x_size,
                                                 requires_grad=True)
            self.ensure_shared_weights()
        else:
            self.shared_weights = None

        # Regenerate context but keep the object ID
        if not hasattr(self, 'analog_ctx'):  # when loading
            self.analog_ctx = AnalogContext(self, parameter=analog_ctx)
        self.analog_ctx.reset(self)
        self.analog_ctx.set_data(analog_ctx.data)

        if to_device.type.startswith('cuda'):
            self.cuda(to_device)

        if alpha_scale is not None:
            # legacy. We apply the alpha scale instaed of the
            # out_scaling_alpha when loading. The alpha_scale
            # mechansim is now replaced with the out scaling factors
            #
            # Caution: will overwrite the loaded out_scaling_alphas
            # if they would exist also (should not be for old checkpoints)

            self.set_out_scaling_alpha(alpha_scale)
Esempio n. 28
0
 def _get_device(self):
     return torch_device("cuda" if self.is_cuda else "cpu")
Esempio n. 29
0
def main():
    temp_size = 5
    future_size = 0
    net_temp_output = future_size + 1
    out_dims = (260, 346)
    batch_size = 12
    num_classes = 3
    # use_gpu = torch_cuda.is_available()
    use_gpu = False     # pytorch is giving a memory error
    device = torch_device(torch_cuda.current_device()) if use_gpu else torch_device("cpu")

    with open("cfg.txt") as fd:
        data_specs = json.load(fd)

    r = get_images_classes(
        data_specs['images_path'],
        data_specs['info_dict'],
        data_specs['class_of_interest']
    )

    train_d, val_d, test_d = split_data(
        data_specs['positive_ev_path'],
        r,
        window_size=temp_size,
        future_size=future_size
    )

    info_path = data_specs["info_path"]
    train_ds = PixelLevelDs(
        train_d,
        info_path=info_path
    )
    val_ds = PixelLevelDs(
        val_d,
        # add_polarity="neg",
        info_path=info_path
    )
    test_ds = PixelLevelDs(
        test_d,
        info_path=info_path
    )

    dls = {
        "train": DataLoader(train_ds, batch_size=batch_size, num_workers=0, pin_memory=use_gpu),
        "val": DataLoader(val_ds, batch_size=batch_size, num_workers=0, pin_memory=use_gpu),
        "test": DataLoader(test_ds, batch_size=1)
    }

    full_forward = SaveStatsForwardCallback(heatmap_CE_accuracy(num_classes))
    # to_device = ToDeviceCallback(device)
    compose_callbacks = ComposeCallback(
        start_callbacks=[StartPrinterCallback()],
        train_pData_callbacks=[
            # to_device,
            full_forward],
        val_pData_callbacks=[
            # to_device,
            full_forward],
        nextEpoch_callbacks=[full_forward],
        phaseEnded_callbacks=[full_forward],
        allEnded_callbacks=[]
    )
    # TODO: read <A Closer Look at Spatiotemporal Convolutions for Action Recognition>
    #  https://arxiv.org/pdf/1711.11248.pdf

    model = ToNameNet(temp_size, net_temp_output, out_dims, num_classes)
    model.to(device)

    train(model, dls['train'], dls['val'], compose_callbacks,
          # criterion=heatmap_mse(num_classes),
          criterion=heatmap_cross_entropy(num_classes, weight=tensor([0.3, 4, 15], dtype=torch_float)),
          epochs=1)
Esempio n. 30
0
do_3state = True
if model_type == '2state':
    do_3state = False
elif model_type == '3state':
    do_3state = True
else:
    print('Provide a model type (2state or 3state) as an argument')
# flag that indicates on which state the trajectory is running
flag_es = 3
# flags for hopping
do_hop = True
hop = False
skip_next = False

# SchNet calculator
model_s = load_model(path1, map_location=torch_device('cpu'))
model2_s = load_model(path2, map_location=torch_device('cpu'))
if do_3state:
    model3_s = load_model(path3, map_location=torch_device('cpu'))

model = model_s.double()
model2 = model2_s.double()
if do_3state:
    model3 = model3_s.double()

# demon-nano calculator
input_arguments = {
    'DFTB': 'SCC LRESP EXST=2',
    'CHARGE': '0.0',
    'PARAM': 'PTYPE=BIO'
}