10601-hws/HW 6/hw6/python/cnn_lenet.py
def pooling_layer_forward(input, layer):
  """Pooling forward

  Args:
    input: a dictionary contains output data and shape information
    layer: one cnn layer, defined in testLeNet.py

  Returns:
    output: a dictionary contains output data and shape information

The output of max-pooling is a (d-by-n) matrix, where n is the number of images in one mini-batch, and d is computed by (width * height * channel). More specifically, the format of each column of the output is:
channel 1 at (x = 1, y = 1)
channel 2 at (x = 1, y = 1)
.
.
channel c at (x = 1, y = 1)
channel 1 at (x = 2, y = 1)
channel 2 at (x = 2, y = 1)

Example sizes:
      input['data'].shape --> (3200, 10000)
      input_n['data'].shape --> (3200,) (one batch size of it)
      col.shape --> (200, 16) (reshaped to have
      output['data'].shape--> (800, 10000)

  output - {'width': 24, 'data': array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     ...,
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.]]), 'batch_size': 64, 'channel': 20, 'height': 24}

  """

  h_in = input['height']
  w_in = input['width']
  c = input['channel']
  batch_size = input['batch_size']
  k = layer['k']
  pad = layer['pad']
  stride = layer['stride']

  h_out = (h_in + 2*pad - k) / stride + 1
  w_out = (w_in + 2*pad - k) / stride + 1

  assert h_out == np.floor(h_out), 'h_out is not integer (error occured at pooling forward)'
  assert w_out == np.floor(w_out), 'w_out is not integer (error occured at pooling forward)'

  output = {}
  output['height'] = h_out
  output['width'] = w_out
  output['channel'] = c
  output['batch_size'] = batch_size
  output['data'] = np.zeros((h_out * w_out * c, batch_size))

  # TODO: implement your pooling forward here
  # implementation begins
  input_n = {
    'height': h_in,
    'width': w_in,
    'channel': c,
  }

  for n in range(batch_size):
    input_n['data'] = input['data'][:, n]
    col = im2col_conv(input_n, layer, h_out, w_out)
    col = np.reshape(col, (k*k, c, h_out * w_out))
    tmp_output = col.max(axis=0)
    output['data'][:,n] = tmp_output.T.flatten()

    # for channel in range(c):
    #     tmp_output = np.append(tmp_output, np.amax(col[channel::input_n['channel']], axis=0))

    # implementation ends
  assert np.all(output['data'].shape == (h_out * w_out * c, batch_size)), 'output[\'data\'] has incorrect shape! (error occured at pooling forward)'
  return output
Pooling forward

  Args:
    input: a dictionary contains output data and shape information
    layer: one cnn layer, defined in testLeNet.py

  Returns:
    output: a dictionary contains output data and shape information

The output of max-pooling is a (d-by-n) matrix, where n is the number of images in one mini-batch, and d is computed by (width * height * channel). More specifically, the format of each column of the output is:
channel 1 at (x = 1, y = 1)
channel 2 at (x = 1, y = 1)
.
.
channel c at (x = 1, y = 1)
channel 1 at (x = 2, y = 1)
channel 2 at (x = 2, y = 1)

Example sizes:
      input['data'].shape --> (3200, 10000)
      input_n['data'].shape --> (3200,) (one batch size of it)
      col.shape --> (200, 16) (reshaped to have
      output['data'].shape--> (800, 10000)

  output - {'width': 24, 'data': array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     ...,
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.]]), 'batch_size': 64, 'channel': 20, 'height': 24}

  
Pooling forward
Args:
input: a dictionary contains output data and shape information
layer: one cnn layer, defined in testLeNet.py

a dictionary contains output data and shape information

The output of max-pooling is a (d-by-n) matrix, where n is the number of images in one mini-batch, and d is computed by (width * height * channel).




Pooling forward Args : input : a dictionary contains output data and shape information layer : one cnn layer defined in testLeNet . py a dictionary contains output data and shape information The output of max - pooling is a ( d - by - n ) matrix where n is the number of images in one mini - batch and d is computed by ( width * height * channel ) .
def pooling_layer_forward(input, layer):
  h_in = input['height']
  w_in = input['width']
  c = input['channel']
  batch_size = input['batch_size']
  k = layer['k']
  pad = layer['pad']
  stride = layer['stride']
  h_out = (h_in + 2*pad - k) / stride + 1
  w_out = (w_in + 2*pad - k) / stride + 1
  assert h_out == np.floor(h_out), 'h_out is not integer (error occured at pooling forward)'
  assert w_out == np.floor(w_out), 'w_out is not integer (error occured at pooling forward)'
  output = {}
  output['height'] = h_out
  output['width'] = w_out
  output['channel'] = c
  output['batch_size'] = batch_size
  output['data'] = np.zeros((h_out * w_out * c, batch_size))
  input_n = {
    'height': h_in,
    'width': w_in,
    'channel': c,
  }
  for n in range(batch_size):
    input_n['data'] = input['data'][:, n]
    col = im2col_conv(input_n, layer, h_out, w_out)
    col = np.reshape(col, (k*k, c, h_out * w_out))
    tmp_output = col.max(axis=0)
    output['data'][:,n] = tmp_output.T.flatten()
  assert np.all(output['data'].shape == (h_out * w_out * c, batch_size)), 'output[\'data\'] has incorrect shape! (error occured at pooling forward)'
  return output
def pooling_layer_forward ( input , layer ) : h_in = input [ 'height' ] w_in = input [ 'width' ] c = input [ 'channel' ] batch_size = input [ 'batch_size' ] k = layer [ 'k' ] pad = layer [ 'pad' ] stride = layer [ 'stride' ] h_out = ( h_in + 2 * pad - k ) / stride + 1 w_out = ( w_in + 2 * pad - k ) / stride + 1 assert h_out == np . floor ( h_out ) , 'h_out is not integer (error occured at pooling forward)' assert w_out == np . floor ( w_out ) , 'w_out is not integer (error occured at pooling forward)' output = { } output [ 'height' ] = h_out output [ 'width' ] = w_out output [ 'channel' ] = c output [ 'batch_size' ] = batch_size output [ 'data' ] = np . zeros ( ( h_out * w_out * c , batch_size ) ) input_n = { 'height' : h_in , 'width' : w_in , 'channel' : c , } for n in range ( batch_size ) : input_n [ 'data' ] = input [ 'data' ] [ : , n ] col = im2col_conv ( input_n , layer , h_out , w_out ) col = np . reshape ( col , ( k * k , c , h_out * w_out ) ) tmp_output = col . max ( axis = 0 ) output [ 'data' ] [ : , n ] = tmp_output . T . flatten ( ) assert np . all ( output [ 'data' ] . shape == ( h_out * w_out * c , batch_size ) ) , 'output[\'data\'] has incorrect shape! (error occured at pooling forward)' return output
Pooling forward
Args:
input: a dictionary contains output data and shape information
layer: one cnn layer, defined in testLeNet.py
Pooling forward Args : input : a dictionary contains output data and shape information layer : one cnn layer defined in testLeNet . py
"""Pooling forward

  Args:
    input: a dictionary contains output data and shape information
    layer: one cnn layer, defined in testLeNet.py

  Returns:
    output: a dictionary contains output data and shape information

The output of max-pooling is a (d-by-n) matrix, where n is the number of images in one mini-batch, and d is computed by (width * height * channel). More specifically, the format of each column of the output is:
channel 1 at (x = 1, y = 1)
channel 2 at (x = 1, y = 1)
.
.
channel c at (x = 1, y = 1)
channel 1 at (x = 2, y = 1)
channel 2 at (x = 2, y = 1)

Example sizes:
      input['data'].shape --> (3200, 10000)
      input_n['data'].shape --> (3200,) (one batch size of it)
      col.shape --> (200, 16) (reshaped to have
      output['data'].shape--> (800, 10000)

  output - {'width': 24, 'data': array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     ...,
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.],
     [ 0.,  0.,  0., ...,  0.,  0.,  0.]]), 'batch_size': 64, 'channel': 20, 'height': 24}

  """ # TODO: implement your pooling forward here # implementation begins # for channel in range(c): #     tmp_output = np.append(tmp_output, np.amax(col[channel::input_n['channel']], axis=0)) # implementation ends
input
layer