Example #1
0
    def test_is_pickleable(self, filters, strides):
        model = CNNModel(filters=filters,
                         strides=strides,
                         name='cnn_model',
                         padding='VALID',
                         hidden_w_init=tf.constant_initializer(1),
                         hidden_nonlinearity=None)
        outputs = model.build(self._input_ph).outputs
        with tf.compat.v1.variable_scope('cnn_model/cnn/h0', reuse=True):
            bias = tf.compat.v1.get_variable('bias')
        bias.load(tf.ones_like(bias).eval())

        output1 = self.sess.run(outputs,
                                feed_dict={self._input_ph: self.obs_input})
        h = pickle.dumps(model)
        with tf.compat.v1.Session(graph=tf.Graph()) as sess:
            model_pickled = pickle.loads(h)
            # pylint: disable=unsubscriptable-object
            input_shape = self.obs_input.shape[1:]  # height, width, channel
            input_ph = tf.compat.v1.placeholder(tf.float32,
                                                shape=(None, ) + input_shape,
                                                name='input')
            outputs = model_pickled.build(input_ph).outputs
            output2 = sess.run(outputs, feed_dict={input_ph: self.obs_input})

            assert np.array_equal(output1, output2)
Example #2
0
    def test_output_value(self, filters, in_channels, strides):
        model = CNNModel(filters=filters,
                         strides=strides,
                         name='cnn_model',
                         padding='VALID',
                         hidden_w_init=tf.constant_initializer(1),
                         hidden_nonlinearity=None)

        outputs = model.build(self._input_ph).outputs
        output = self.sess.run(outputs,
                               feed_dict={self._input_ph: self.obs_input})

        filter_sum = 1
        # filter value after 3 layers of conv
        for filter_iter, in_channel in zip(filters, in_channels):
            filter_sum *= filter_iter[1][0] * filter_iter[1][1] * in_channel

        height_size = self.input_height
        width_size = self.input_width
        for filter_iter, stride in zip(filters, strides):
            height_size = int((height_size - filter_iter[1][0]) / stride) + 1
            width_size = int((width_size - filter_iter[1][1]) / stride) + 1
        flatten_shape = height_size * width_size * filters[-1][0]

        # flatten
        expected_output = np.full((self.batch_size, flatten_shape),
                                  filter_sum,
                                  dtype=np.float32)

        assert np.array_equal(output, expected_output)
Example #3
0
    def test_is_pickleable(self, filter_sizes, in_channels, out_channels,
                           strides):
        model = CNNModel(filter_dims=filter_sizes,
                         num_filters=out_channels,
                         strides=strides,
                         name='cnn_model',
                         padding='VALID',
                         hidden_w_init=tf.constant_initializer(1),
                         hidden_nonlinearity=None)
        outputs = model.build(self._input_ph)
        with tf.variable_scope('cnn_model/cnn/h0', reuse=True):
            bias = tf.get_variable('bias')
        self.sess.run(tf.assign(bias, tf.ones_like(bias)))

        output1 = self.sess.run(outputs,
                                feed_dict={self._input_ph: self.obs_input})
        h = pickle.dumps(model)
        with tf.Session(graph=tf.Graph()) as sess:
            model_pickled = pickle.loads(h)
            input_shape = self.obs_input.shape[1:]  # height, width, channel
            input_ph = tf.placeholder(tf.float32,
                                      shape=(None, ) + input_shape,
                                      name='input')
            outputs = model_pickled.build(input_ph)
            output2 = sess.run(outputs, feed_dict={input_ph: self.obs_input})

            assert np.array_equal(output1, output2)