def test(self):

        with self.test_session() as sess:

            m = tf.constant(np.array([
                [1.0, 2.0],
                [2.0, 0.0]
            ], dtype=np.float32))

            l = linear(m, 4)

            result = sess.run(l, {
                'SimpleLinear/Matrix:0': np.array([
                    [1.0, 2.0],
                    [1.0, 2.0],
                    [1.0, 2.0],
                    [1.0, 2.0],
                ]),
                'SimpleLinear/Bias:0': np.array([
                    0.0,
                    1.0,
                    2.0,
                    3.0,
                ]),
            })

            self.assertAllClose(result, np.array([
                [5.0, 6.0, 7.0, 8.0],
                [2.0, 3.0, 4.0, 5.0],
            ]))
            print(result)
Exemple #2
0
    def test_1(self):
        # for now test only running to completion
        verbose = False
        num_train = 2000
        num_validate = 100
        num_dimensions = 3
        error_variance = 1e-2
        l1 = 0.0
        l2 = 0.001
        gradient, loss, predict = model.linear(l1, l2)
        theta_actual, train_x, train_y, validate_x, validate_y = \
            self.generate_data(num_train,
                               num_validate,
                               num_dimensions,
                               error_variance,
                               predict)

        if verbose:
            print 'theta actual', theta_actual
            print 'error_variance', error_variance
            print 'bayes RMSE', self.bayes_rmse(theta_actual,
                                                train_x, train_y,
                                                predict)

        theta0 = np.zeros(num_dimensions + 1)
        theta_star = sgd_batch_auto(l1 + l2,
                                    gradient,
                                    loss,
                                    theta0,
                                    train_x, train_y, validate_x, validate_y,
                                    int(num_train * .1))
        if verbose:
            print 'theta_star', theta_star
        diff = np.linalg.norm(theta_actual - theta_star)
        self.assertLess(diff, 2e-2)
Exemple #3
0
    def forward(self, x):
        outs = []
        for l in self.conv1s:
            out = pad_layer(x, l)
            outs.append(out)
        out = torch.cat(outs + [x], dim=1)
        out = F.leaky_relu(out, negative_slope=self.ns)
        out = self.conv_block(out, [self.conv2], [self.ins_norm1, self.drop1],
                              res=False)
        out = self.conv_block(out, [self.conv3, self.conv4],
                              [self.ins_norm2, self.drop2])
        out = self.conv_block(out, [self.conv5, self.conv6],
                              [self.ins_norm3, self.drop3])
        out = self.conv_block(out, [self.conv7, self.conv8],
                              [self.ins_norm4, self.drop4])
        # dense layer
        out = self.dense_block(out, [self.dense1, self.dense2],
                               [self.ins_norm5, self.drop5],
                               res=True)
        out = self.dense_block(out, [self.dense3, self.dense4],
                               [self.ins_norm6, self.drop6],
                               res=True)
        out_rnn = RNN(out, self.RNN)
        out = torch.cat([out, out_rnn], dim=1)
        out = linear(out, self.linear)

        mean = RNN(out, self.mean)
        log_var = RNN(out, self.log_var)

        if self.one_hot:
            out = gumbel_softmax(out)
        else:
            out = F.leaky_relu(out, negative_slope=self.ns)
        return out, mean, log_var
Exemple #4
0
    def find_thetas(self, l1, l2):
        '''return dict of optimal theta values for increasing num features'''
        verbose = True
        gradient, loss, predict = model.linear(l1, l2)

        result = {}
        num_features_low = 1
        num_features_low = 9
        num_features_high = 9
        for num_features in xrange(num_features_low, num_features_high + 1):
            # NOTE: not sure that model_linear works if no x values
            # transform generated random x values into features
            self.train_x = self.phi(self.tx, num_features)
            self.validate_x = self.phi(self.vx, num_features)

            theta_star = self.fit(l1, l2, gradient, loss)
            error = self.mean_loss(theta_star, loss)
            if verbose:
                print num_features, theta_star, error
            result[num_features] = (theta_star, error)

        print 'num_features, theta_star'
        for num_features in sorted(result):
            theta_star, error = result[num_features]
            print num_features, theta_star

        print 'num_features, error'
        for num_features in sorted(result):
            theta_star, error = result[num_features]
            print num_features, error
        pdb.set_trace()
        return result
Exemple #5
0
 def dense_block(self, x, emb, layers, norm_layer, res=True):
     out = x
     for layer in layers:
         out = out + emb.view(emb.size(0), emb.size(1), 1)
         out = linear(out, layer)
         out = F.leaky_relu(out, negative_slope=self.ns)
     out = norm_layer(out)
     if res:
         out = out + x
     return out
Exemple #6
0
 def dense_block(self, x, layers, norm_layers, res=True):
     out = x
     for layer in layers:
         out = linear(out, layer)
         out = F.leaky_relu(out, negative_slope=self.ns)
     for layer in norm_layers:
         out = layer(out)
     if res:
         out = out + x
     return out
Exemple #7
0
def test_linear() -> None:
    with tf.Graph().as_default(), tf.Session() as session, tf.variable_scope(
            "test", reuse=tf.AUTO_REUSE):
        output = model.linear(tf.constant([[1, 2, 3]], dtype=tf.float32),
                              2,
                              use_bias=True)
        session.run(tf.global_variables_initializer())
        session.run(
            tf.assign(tf.get_variable("linear/weight"),
                      [[0, 0], [1, 0], [0, 10]]))
        np.testing.assert_allclose(session.run(output), [[2, 30]])
Exemple #8
0
    def forward(self, x, c):
        # conv layer
        x, z, log_var = x
        x = x * (torch.exp(.5 * log_var)) + z

        out = self.conv_block(x, [self.conv1, self.conv2],
                              self.ins_norm1,
                              self.emb1(c),
                              res=True)
        out = self.conv_block(out, [self.conv3, self.conv4],
                              self.ins_norm2,
                              self.emb2(c),
                              res=True)
        out = self.conv_block(out, [self.conv5, self.conv6],
                              self.ins_norm3,
                              self.emb3(c),
                              res=True)
        # dense layer
        out = self.dense_block(out,
                               self.emb4(c), [self.dense1, self.dense2],
                               self.ins_norm4,
                               res=True)
        out = self.dense_block(out,
                               self.emb4(c), [self.dense3, self.dense4],
                               self.ins_norm5,
                               res=True)
        emb = self.emb5(c)
        out_add = out + emb.view(emb.size(0), emb.size(1), 1)
        # rnn layer
        out_rnn = RNN(out_add, self.RNN)
        out = torch.cat([out, out_rnn], dim=1)
        out = append_emb(self.emb5(c), out.size(2), out)
        out = linear(out, self.dense5)
        out = F.leaky_relu(out, negative_slope=self.ns)
        out = linear(out, self.linear)
        #out = torch.tanh(out)
        return out
def test_linear():
    h = 2
    sill = 1
    range = 3
    assert model.linear(h, sill, range) == 2 / 3
Exemple #10
0
from glob import glob
import cv2
from utils import path_to_tensor
from keras.applications import *
from model import get_features, linear, pretrain
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True



inception_sz = (1536,)
X = np.zeros((299, 299, 3), dtype=np.float32)
sorted_breed = np.load('breed_sorted.npy')

pretrain_model = pretrain(InceptionResNetV2)
classifier = linear(inp_sz=inception_sz, out_sz=120)
classifier.load_weights('weights.best.inceptresnetv2.comb.hdf5')

pred_images = np.array(glob("pic_pred/*"))

for dog_pic in pred_images:
    time_loop = time.time()
    tensor = path_to_tensor(dog_pic)
    time_vectorize = time.time()
    features = pretrain_model.predict(tensor, batch_size=256, verbose=1)
    time_feature = time.time()
    y_pred = classifier.predict(features, batch_size=1)
    pred = np.argmax(y_pred, axis=1)
    time_classify = time.time()
    print(dog_pic, "is a ", sorted_breed[pred])
    print("vectorize time", time_vectorize - time_loop)