def load_model(self):
        """
        Load model.
        """

        database = utils.read_hdf5(self.args.database_file).astype(numpy.float32)
        log('[Attack] read %sd' % self.args.database_file)

        self.N_font = database.shape[0]
        self.N_class = database.shape[1]
        resolution = database.shape[2]

        database = database.reshape((database.shape[0] * database.shape[1], database.shape[2], database.shape[3]))
        database = torch.from_numpy(database)
        if self.args.use_gpu:
            database = database.cuda()
        database = torch.autograd.Variable(database, False)

        N_theta = self.test_theta.shape[1]
        log('[Attack] using %d N_theta' % N_theta)
        decoder = models.AlternativeOneHotDecoder(database, self.N_font, self.N_class, N_theta)
        decoder.eval()

        image_channels = 1 if N_theta <= 7 else 3
        network_units = list(map(int, self.args.network_units.split(',')))
        log('[Attack] using %d input channels' % image_channels)
        classifier = models.Classifier(self.N_class, resolution=(image_channels, resolution, resolution),
                                       architecture=self.args.network_architecture,
                                       activation=self.args.network_activation,
                                       batch_normalization=not self.args.network_no_batch_normalization,
                                       start_channels=self.args.network_channels,
                                       dropout=self.args.network_dropout,
                                       units=network_units)

        assert os.path.exists(self.args.classifier_file), 'state file %s not found' % self.args.classifier_file
        state = State.load(self.args.classifier_file)
        log('[Attack] read %s' % self.args.classifier_file)

        classifier.load_state_dict(state.model)
        if self.args.use_gpu and not cuda.is_cuda(classifier):
            log('[Attack] classifier is not CUDA')
            classifier = classifier.cuda()
        log('[Attack] loaded classifier')

        # !
        classifier.eval()
        log('[Attack] set classifier to eval')

        self.model = models.DecoderClassifier(decoder, classifier)
    def load_decoder(self):
        """
        Load the decoder.
        """

        N_theta = self.test_theta.shape[1]
        log('[Training] using %d N_theta' % N_theta)
        self.decoder = models.AlternativeOneHotDecoder(self.database,
                                                       self.N_font,
                                                       self.N_class, N_theta)
        self.decoder.eval()
        log('[Training] set up decoder')

        self.decoder_classifier = models.DecoderClassifier(
            self.decoder, self.model)
예제 #3
0
    def load_decoder(self):
        """
        Load the decoder.
        """

        assert self.args.N_theta > 0 and self.args.N_theta <= 9

        min_translation_x, max_translation_x = map(
            float, self.args.translation_x.split(','))
        min_translation_y, max_translation_y = map(
            float, self.args.translation_y.split(','))
        min_shear_x, max_shear_x = map(float, self.args.shear_x.split(','))
        min_shear_y, max_shear_y = map(float, self.args.shear_y.split(','))
        min_scale, max_scale = map(float, self.args.scale.split(','))
        min_rotation, max_rotation = map(float, self.args.rotation.split(','))
        min_color, max_color = self.args.color, 1

        self.min_bound = numpy.array([
            min_translation_x,
            min_translation_y,
            min_shear_x,
            min_shear_y,
            min_scale,
            min_rotation,
            min_color,
            min_color,
            min_color,
        ])
        self.max_bound = numpy.array([
            max_translation_x, max_translation_y, max_shear_x, max_shear_y,
            max_scale, max_rotation, max_color, max_color, max_color
        ])

        self.min_bound = self.min_bound[:self.args.N_theta].astype(
            numpy.float32)
        self.max_bound = self.max_bound[:self.args.N_theta].astype(
            numpy.float32)

        self.decoder = models.STNDecoder(self.args.N_theta)
        log('[Training] set up STN decoder')
        self.decoder_classifier = models.DecoderClassifier(
            self.decoder, self.model)
        log('[Training] set up decoder classifier')
예제 #4
0
    def load_decoder(self):
        """
        Load the decoder.
        """

        assert self.args.decoder_files
        decoder_files = self.args.decoder_files.split(',')
        for decoder_file in decoder_files:
            assert os.path.exists(
                decoder_file), 'could not find %s' % decoder_file

        log('[Training] using %d input channels' % self.train_images.shape[3])
        decoder_units = list(map(int, self.args.decoder_units.split(',')))

        if len(decoder_files) > 1:
            log('[Training] loading multiple decoders')
            decoders = []
            for i in range(len(decoder_files)):
                decoder = models.LearnedDecoder(
                    self.args.latent_space_size,
                    resolution=(self.train_images.shape[3],
                                self.train_images.shape[1],
                                self.train_images.shape[2]),
                    architecture=self.args.decoder_architecture,
                    start_channels=self.args.decoder_channels,
                    activation=self.args.decoder_activation,
                    batch_normalization=not self.args.
                    decoder_no_batch_normalization,
                    units=decoder_units)

                state = State.load(decoder_files[i])
                decoder.load_state_dict(state.model)
                if self.args.use_gpu and not cuda.is_cuda(decoder):
                    decoder = decoder.cuda()
                decoders.append(decoder)

                decoder.eval()
                log('[Training] loaded %s' % decoder_files[i])
            self.decoder = models.SelectiveDecoder(
                decoders,
                resolution=(self.train_images.shape[3],
                            self.train_images.shape[1],
                            self.train_images.shape[2]))
        else:
            log('[Training] loading one decoder')
            decoder = models.LearnedDecoder(
                self.args.latent_space_size,
                resolution=(self.train_images.shape[3],
                            self.train_images.shape[1],
                            self.train_images.shape[2]),
                architecture=self.args.decoder_architecture,
                start_channels=self.args.decoder_channels,
                activation=self.args.decoder_activation,
                batch_normalization=not self.args.
                decoder_no_batch_normalization,
                units=decoder_units)

            state = State.load(decoder_files[0])
            decoder.load_state_dict(state.model)
            if self.args.use_gpu and not cuda.is_cuda(decoder):
                decoder = decoder.cuda()
            decoder.eval()
            log('[Training] read decoder')

            self.decoder = decoder

        self.decoder_classifier = models.DecoderClassifier(
            self.decoder, self.model)
예제 #5
0
    def load_model(self):
        """
        Load model.
        """

        assert self.args.decoder_files
        decoder_files = self.args.decoder_files.split(',')
        for decoder_file in decoder_files:
            assert os.path.exists(
                decoder_file), 'could not find %s' % decoder_file

        decoder_units = list(map(int, self.args.decoder_units.split(',')))
        log('[Attack] using %d input channels' % self.test_images.shape[3])

        if len(decoder_files) > 1:
            log('[Attack] loading multiple decoders')
            decoders = []
            for i in range(len(decoder_files)):
                decoder = models.LearnedDecoder(
                    self.args.latent_space_size,
                    resolution=(self.test_images.shape[3],
                                self.test_images.shape[1],
                                self.test_images.shape[2]),
                    architecture=self.args.decoder_architecture,
                    start_channels=self.args.decoder_channels,
                    activation=self.args.decoder_activation,
                    batch_normalization=not self.args.
                    decoder_no_batch_normalization,
                    units=decoder_units)
                log(decoder)
                state = State.load(decoder_files[i])
                decoder.load_state_dict(state.model)
                if self.args.use_gpu and not cuda.is_cuda(decoder):
                    decoder = decoder.cuda()
                decoders.append(decoder)

                decoder.eval()
                log('[Attack] loaded %s' % decoder_files[i])
            decoder = models.SelectiveDecoder(
                decoders,
                resolution=(self.test_images.shape[3],
                            self.test_images.shape[1],
                            self.test_images.shape[2]))
        else:
            log('[Attack] loading one decoder')
            decoder = models.LearnedDecoder(
                self.args.latent_space_size,
                resolution=(self.test_images.shape[3],
                            self.test_images.shape[1],
                            self.test_images.shape[2]),
                architecture=self.args.decoder_architecture,
                start_channels=self.args.decoder_channels,
                activation=self.args.decoder_activation,
                batch_normalization=not self.args.
                decoder_no_batch_normalization,
                units=decoder_units)

            state = State.load(decoder_files[0])
            decoder.load_state_dict(state.model)
            if self.args.use_gpu and not cuda.is_cuda(decoder):
                decoder = decoder.cuda()
            decoder.eval()
            log('[Attack] read decoder')

        classifier_units = list(map(int, self.args.network_units.split(',')))
        classifier = models.Classifier(
            self.N_class,
            resolution=(self.test_images.shape[3], self.test_images.shape[1],
                        self.test_images.shape[2]),
            architecture=self.args.network_architecture,
            activation=self.args.network_activation,
            batch_normalization=not self.args.network_no_batch_normalization,
            start_channels=self.args.network_channels,
            dropout=self.args.network_dropout,
            units=classifier_units)

        assert os.path.exists(
            self.args.classifier_file
        ), 'state file %s not found' % self.args.classifier_file
        state = State.load(self.args.classifier_file)
        log('[Attack] read %s' % self.args.classifier_file)

        classifier.load_state_dict(state.model)
        if self.args.use_gpu and not cuda.is_cuda(classifier):
            log('[Attack] classifier is not CUDA')
            classifier = classifier.cuda()
        log('[Attack] loaded classifier')

        # !
        classifier.eval()
        log('[Attack] set classifier to eval')

        self.model = models.DecoderClassifier(decoder, classifier)
예제 #6
0
    def load_model(self):
        """
        Load the decoder.
        """

        assert self.args.N_theta > 0 and self.args.N_theta <= 9

        min_translation_x, max_translation_x = map(
            float, self.args.translation_x.split(','))
        min_translation_y, max_translation_y = map(
            float, self.args.translation_y.split(','))
        min_shear_x, max_shear_x = map(float, self.args.shear_x.split(','))
        min_shear_y, max_shear_y = map(float, self.args.shear_y.split(','))
        min_scale, max_scale = map(float, self.args.scale.split(','))
        min_rotation, max_rotation = map(float, self.args.rotation.split(','))
        min_color, max_color = self.args.color, 1

        self.min_bound = numpy.array([
            min_translation_x,
            min_translation_y,
            min_shear_x,
            min_shear_y,
            min_scale,
            min_rotation,
            min_color,
            min_color,
            min_color,
        ])
        self.max_bound = numpy.array([
            max_translation_x, max_translation_y, max_shear_x, max_shear_y,
            max_scale, max_rotation, max_color, max_color, max_color
        ])

        self.min_bound = self.min_bound[:self.args.N_theta].astype(
            numpy.float32)
        self.max_bound = self.max_bound[:self.args.N_theta].astype(
            numpy.float32)

        decoder = models.STNDecoder(self.args.N_theta)
        log('[Attack] set up STN decoder')

        classifier = models.Classifier(
            self.N_class,
            resolution=(self.test_images.shape[3], self.test_images.shape[1],
                        self.test_images.shape[2]),
            architecture='standard',
            activation=self.args.network_activation,
            batch_normalization=not self.args.network_no_batch_normalization,
            start_channels=self.args.network_channels,
            dropout=self.args.network_dropout)

        assert os.path.exists(
            self.args.classifier_file
        ), 'state file %s not found' % self.args.classifier_file
        state = State.load(self.args.classifier_file)
        log('[Attack] read %s' % self.args.classifier_file)

        classifier.load_state_dict(state.model)
        if self.args.use_gpu and not cuda.is_cuda(classifier):
            log('[Attack] classifier is not CUDA')
            classifier = classifier.cuda()
        classifier.eval()
        log('[Attack] loaded classifier')

        self.model = models.DecoderClassifier(decoder, classifier)
        log('[Training] set up decoder classifier')