def forward(self, input): # Create a placeholder for videos. k = self.batch_size inputs = tf.placeholder(tf.float32, [self.batch_size, self.timesteps] + [] + [channels]) cell = ConvLSTMCell(self.shape, self.filters, self.kernel) outputs, state = tf.nn.dynamic_rnn(cell, inputs, dtype=inputs.dtype) # There's also a ConvGRUCell that is more memory efficient. from cell import ConvGRUCell cell = ConvGRUCell(shape, filters, kernel) outputs, state = tf.nn.dynamic_rnn(cell, inputs, dtype=inputs.dtype) # It's also possible to enter 2D input or 4D input instead of 3D. shape = [100] kernel = [3] inputs = tf.placeholder(tf.float32, [batch_size, timesteps] + shape + [channels]) cell = ConvLSTMCell(shape, filters, kernel) outputs, state = tf.nn.bidirectional_dynamic_rnn(cell, cell, inputs, dtype=inputs.dtype) shape = [50, 50, 50] kernel = [1, 3, 5] inputs = tf.placeholder(tf.float32, [batch_size, timesteps] + shape + [channels]) cell = ConvGRUCell(shape, filters, kernel) outputs, state = tf.nn.bidirectional_dynamic_rnn(cell, cell, inputs, dtype=inputs.dtype)
def t2(): # There's also a ConvGRUCell that is more memory efficient. cell = ConvGRUCell(shape, filters, kernel) outputs, state = tf.nn.dynamic_rnn(cell, inputs, dtype=inputs.dtype) with tf.Session() as sess: inp = np.random.normal(size=(batch_size, timesteps, height, width, channels)) sess.run(tf.global_variables_initializer()) output, cell_and_hidden_state = sess.run([outputs, state], feed_dict={inputs: inp}) print("output shape:", output.shape ) # output:[time, batch_size, height, width, width, num_filter] print("cell_and_hidden_state:", cell_and_hidden_state)
def t4(): shape = [50, 50, 50] kernel = [1, 3, 5] inputs = tf.placeholder(tf.float32, [batch_size, timesteps] + shape + [channels]) cell = ConvGRUCell(shape, filters, kernel) outputs, state = tf.nn.bidirectional_dynamic_rnn(cell, cell, inputs, dtype=inputs.dtype) with tf.Session() as sess: inp = np.random.normal(size=(batch_size, timesteps, height, width, channels)) sess.run(tf.global_variables_initializer()) output, cell_and_hidden_state = sess.run([outputs, state], feed_dict={inputs: inp}) print("output shape:", output.shape ) # output:[time, batch_size, height, width, width, num_filter] print("cell_and_hidden_state:", cell_and_hidden_state)
inputs = tf.placeholder(tf.float32, [batch_size, timesteps] + shape + [channels]) #print(inputs) # Make placeholder batch major again after RNN. (see https://github.com/tensorflow/tensorflow/pull/5142) inputs = tf.transpose(inputs, (1, 0, 2, 3, 4)) #print(inputs) # There's also a ConvGRUCell that is more memory efficient. #from cell import ConvGRUCell #with tf.variable_scope('cell_0'): # def add_cell(shapes, filters, kernels, initializer, reuse=None): # return ConvGRUCell(shapes, filters, kernels, initializer=initializer, reuse=reuse) with tf.variable_scope('cell_0'): cell_0 = ConvGRUCell(shape, filter, kernel, initializer=tf.truncated_normal_initializer(stddev=0.01)) #print(cell_0) outputs, state = tf.nn.dynamic_rnn(cell_0, inputs, initial_state=cell_0.zero_state(inputs.shape[1].value, dtype=tf.float32), dtype=inputs.dtype, time_major=True) outputs = tf.concat([outputs, outputs], 2) #print(outputs) # # with tf.variable_scope('cell_1'): cell_1 = ConvGRUCell(shape, 3, kernel_1, initializer=tf.truncated_normal_initializer(stddev=0.01)) outputs_1, state_1 = tf.nn.dynamic_rnn(cell_1, outputs, initial_state=cell_1.zero_state(outputs.shape[1].value, dtype=tf.float32), dtype=inputs.dtype, time_major=True) with tf.variable_scope('cell_1', reuse=True): cell_3 = ConvGRUCell(shape, 3, kernel_1)#, reuse=True) #print(cell_1) outputs_3, state_3 = tf.nn.dynamic_rnn(cell_3, outputs, initial_state=state_1, dtype=inputs.dtype, time_major=True) print(outputs_1)
kernel = [3, 3] channels = 3 filters = 12 # Create a placeholder for videos. inputs = tf.placeholder(tf.float32, [batch_size, timesteps] + shape + [channels]) # Add the ConvLSTM step. from cell import ConvLSTMCell cell = ConvLSTMCell(shape, filters, kernel) outputs, state = tf.nn.dynamic_rnn(cell, inputs, dtype=inputs.dtype) # There's also a ConvGRUCell that is more memory efficient. from cell import ConvGRUCell cell = ConvGRUCell(shape, filters, kernel) outputs, state = tf.nn.dynamic_rnn(cell, inputs, dtype=inputs.dtype) # It's also possible to enter 2D input or 4D input instead of 3D. shape = [100] kernel = [3] inputs = tf.placeholder(tf.float32, [batch_size, timesteps] + shape + [channels]) cell = ConvLSTMCell(shape, filters, kernel) outputs, state = tf.nn.bidirectional_dynamic_rnn(cell, cell, inputs, dtype=inputs.dtype) shape = [50, 50, 50] kernel = [1, 3, 5]
height = 100 channels = 512 filters = 3 kernel = [3, 3] slice_dimension=3 # Create a placeholder for videos. inputs = tf.placeholder(tf.float32, [batch_size, timesteps, width, height, channels]) y_slice=tf.placeholder(tf.int32,[batch_size,timesteps,slice_dimension]) output_y=tf.placeholder(tf.int32,[batch_size,timesteps,width,height,channels]) # Flatten input because tf.nn.dynamic_rnn only accepts 3D input. inputs = flatten(inputs) # Add the ConvLSTM step. cell = ConvGRUCell(height, width, filters, kernel) outputs, state = tf.nn.dynamic_rnn(cell, inputs, dtype=inputs.dtype) # Reshape outputs to videos again, because tf.nn.dynamic_rnn only accepts 3D input. outputs = expand(outputs, height, width, filters) for o_batch,y_batch in zip(outputs,y_slice): for o_time,y_time in zip(o_batch,y_batch): unpack_outputs=tf.unstack(outputs) unpack_y_slice=tf.unstack(y_slice) out1=tf.slice(outputs[0][0],y_slice[0][0],[10,10,3]) out2=tf.slice(outputs[0][2],y_slice[0][2],[10,10,3]) out4=tf.slice(outputs[0][3],y_slice[0][3],[10,10,3])