def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name='X') net = mx.sym.Convolution(data=net, name='conv', kernel=(7, 7), num_filter=32) net = mx.sym.Activation(data=net, act_type='relu') net = mx.sym.Pooling(data=net, name='pool', pool_type='max', kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) # Create forward function and add parameters to this model. self.conv = Function( net, input_shapes={'X': (batch_size, ) + input_size}, name='conv') self.add_params(self.conv.get_params()) # Define ndarray parameters used for classification part. output_shape = self.conv.get_one_output_shape() conv_out_size = output_shape[1] self.add_param(name='w1', shape=(conv_out_size, hidden_size)) \ .add_param(name='b1', shape=(hidden_size,)) \ .add_param(name='w2', shape=(hidden_size, num_classes)) \ .add_param(name='b2', shape=(num_classes,))
def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name='X') net = mx.sym.Convolution(data=net, name='conv', kernel=(7, 7), num_filter=32) net = mx.sym.Activation(data=net, act_type='relu') net = mx.sym.Pooling(data=net, name='pool', pool_type='max', kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) net = mx.sym.FullyConnected(data=net, name='fc1', num_hidden=hidden_size) net = mx.sym.Activation(data=net, act_type='relu') net = mx.sym.FullyConnected(data=net, name='fc2', num_hidden=num_classes) net = mx.sym.SoftmaxOutput(data=net, name='softmax', normalization='batch') # Create forward function and add parameters to this model. input_shapes = { 'X': (batch_size, ) + input_size, 'softmax_label': (batch_size, ) } self.cnn = Function(net, input_shapes=input_shapes, name='cnn') self.add_params(self.cnn.get_params())
class ConvolutionNet(ModelBase): def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name="X") net = mx.sym.Convolution(data=net, name="conv", kernel=(7, 7), num_filter=32) net = mx.sym.Activation(data=net, act_type="relu") net = mx.sym.Pooling(data=net, name="pool", pool_type="max", kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) # Create forward function and add parameters to this model. self.conv = Function(net, input_shapes={"X": (batch_size,) + input_size}, name="conv") self.add_params(self.conv.get_params()) # Define ndarray parameters used for classification part. output_shape = self.conv.get_one_output_shape() conv_out_size = output_shape[1] self.add_param(name="w1", shape=(conv_out_size, hidden_size)).add_param( name="b1", shape=(hidden_size,) ).add_param(name="w2", shape=(hidden_size, num_classes)).add_param(name="b2", shape=(num_classes,)) def forward(self, X, mode): out = self.conv(X=X, **self.params) out = layers.affine(out, self.params["w1"], self.params["b1"]) out = layers.relu(out) out = layers.affine(out, self.params["w2"], self.params["b2"]) return out def loss(self, predict, y): return layers.softmax_loss(predict, y)
class ConvolutionNet(ModelBase): def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name='X') net = mx.sym.Convolution( data=net, name='conv', kernel=(7, 7), num_filter=32) net = mx.sym.Activation( data=net, act_type='relu') net = mx.sym.Pooling( data=net, name='pool', pool_type='max', kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) net = mx.sym.FullyConnected( data=net, name='fc1', num_hidden=hidden_size) net = mx.sym.Activation( data=net, act_type='relu') net = mx.sym.FullyConnected( data=net, name='fc2', num_hidden=num_classes) net = mx.sym.SoftmaxOutput(data=net, name='softmax', normalization='batch') # Create forward function and add parameters to this model. input_shapes = {'X': (batch_size,) + input_size, 'softmax_label': (batch_size,)} self.cnn = Function(net, input_shapes=input_shapes, name='cnn') self.add_params(self.cnn.get_params()) def forward_batch(self, batch, mode): out = self.cnn(X=batch.data[0], softmax_label=batch.label[0], **self.params) return out def loss(self, predict, y): return layers.softmax_cross_entropy(predict, y)
class ConvolutionNet(ModelBase): def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name="X") net = mx.sym.Convolution(data=net, name="conv", kernel=(7, 7), num_filter=32) net = mx.sym.Activation(data=net, act_type="relu") net = mx.sym.Pooling(data=net, name="pool", pool_type="max", kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) net = mx.sym.FullyConnected(data=net, name="fc1", num_hidden=hidden_size) net = mx.sym.Activation(data=net, act_type="relu") net = mx.sym.FullyConnected(data=net, name="fc2", num_hidden=num_classes) net = mx.sym.SoftmaxOutput(data=net, name="softmax", normalization="batch") # Create forward function and add parameters to this model. input_shapes = {"X": (batch_size,) + input_size, "softmax_label": (batch_size,)} self.cnn = Function(net, input_shapes=input_shapes, name="cnn") self.add_params(self.cnn.get_params()) def forward_batch(self, batch, mode): return self.cnn(X=batch.data[0], softmax_label=batch.label[0], **self.params) def loss(self, predict, y): # Add L2 regularization for all the weights. reg_loss = 0.0 for name, weight in self.params.items(): reg_loss += np.sum(weight ** 2) return layers.softmax_cross_entropy(predict, y) + 0.5 * weight_decay * reg_loss
def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name='X') net = mx.sym.Convolution( data=net, name='conv', kernel=(7, 7), num_filter=32) net = mx.sym.Activation( data=net, act_type='relu') net = mx.sym.Pooling( data=net, name='pool', pool_type='max', kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) # Create forward function and add parameters to this model. self.conv = Function( net, input_shapes={'X': (batch_size,) + input_size}, name='conv') self.add_params(self.conv.get_params()) # Define ndarray parameters used for classification part. output_shape = self.conv.get_one_output_shape() conv_out_size = output_shape[1] self.add_param(name='w1', shape=(conv_out_size, hidden_size)) \ .add_param(name='b1', shape=(hidden_size,)) \ .add_param(name='w2', shape=(hidden_size, num_classes)) \ .add_param(name='b2', shape=(num_classes,))
def batch_dot(left, right): # wraps mxnet.symbol.batch_dot left_symbol = symbol.Variable('left') right_symbol = symbol.Variable('right') result_symbol = symbol.batch_dot(left_symbol, right_symbol) shapes = {'left': left.shape, 'right': right.shape} kwargs = {'left': left, 'right': right} return Function(result_symbol, shapes)(**kwargs)
def batch_dot(left, right): # assert left.shape[0] == right.shape[0] and left.shape[2] == right.shape[1] left_symbol = symbol.Variable('left') right_symbol = symbol.Variable('right') result_symbol = symbol.batch_dot(left_symbol, right_symbol) shapes = {'left': left.shape, 'right': right.shape} kwargs = {'left': left, 'right': right} return Function(result_symbol, shapes)(**kwargs)
def cross_correlation_factory(data_shape, kernel_shape): batch, num_filter, y, x = kernel_shape net = mx.sym.Variable('x') net = mx.sym.Convolution(net, name='conv', kernel=(y, x), num_filter=1, no_bias=True) conv = Function(net, input_shapes={'x': data_shape}) return conv
class ConvolutionNet(ModelBase): def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name='X') net = mx.sym.Convolution(data=net, name='conv', kernel=(7, 7), num_filter=32) net = mx.sym.Activation(data=net, act_type='relu') net = mx.sym.Pooling(data=net, name='pool', pool_type='max', kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) # Create forward function and add parameters to this model. self.conv = Function( net, input_shapes={'X': (batch_size, ) + input_size}, name='conv') self.add_params(self.conv.get_params()) # Define ndarray parameters used for classification part. output_shape = self.conv.get_one_output_shape() conv_out_size = output_shape[1] self.add_param(name='w1', shape=(conv_out_size, hidden_size)) \ .add_param(name='b1', shape=(hidden_size,)) \ .add_param(name='w2', shape=(hidden_size, num_classes)) \ .add_param(name='b2', shape=(num_classes,)) def forward(self, X, mode): out = self.conv(X=X, **self.params) out = layers.affine(out, self.params['w1'], self.params['b1']) out = layers.relu(out) out = layers.affine(out, self.params['w2'], self.params['b2']) # This verifies whether symbols can be reused. trash = self.conv(X=np.zeros(X.shape), **self.params) return out def loss(self, predict, y): return layers.softmax_loss(predict, y)
def cross_correlation(data, kernel): batch, num_filter, y, x = kernel.shape net = mx.sym.Variable('x') net = mx.sym.Convolution(net, name='conv', kernel=(y, x), num_filter=1, no_bias=True) conv = Function(net, input_shapes={'x': data.shape}) #print conv._param_shapes res = conv(x=data, conv_weight=kernel) return res
class ConvolutionNet(ModelBase): def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name='X') net = mx.sym.Convolution( data=net, name='conv', kernel=(7, 7), num_filter=32) net = mx.sym.Activation( data=net, act_type='relu') net = mx.sym.Pooling( data=net, name='pool', pool_type='max', kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) # Create forward function and add parameters to this model. self.conv = Function( net, input_shapes={'X': (batch_size,) + input_size}, name='conv') self.add_params(self.conv.get_params()) # Define ndarray parameters used for classification part. output_shape = self.conv.get_one_output_shape() conv_out_size = output_shape[1] self.add_param(name='w1', shape=(conv_out_size, hidden_size)) \ .add_param(name='b1', shape=(hidden_size,)) \ .add_param(name='w2', shape=(hidden_size, num_classes)) \ .add_param(name='b2', shape=(num_classes,)) def forward(self, X, mode): out = self.conv(X=X, **self.params) out = layers.affine(out, self.params['w1'], self.params['b1']) out = layers.relu(out) out = layers.affine(out, self.params['w2'], self.params['b2']) # This verifies whether symbols can be reused. trash = self.conv(X=np.zeros(X.shape), **self.params) return out def loss(self, predict, y): return layers.softmax_loss(predict, y)
def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name="X") net = mx.sym.Convolution(data=net, name="conv", kernel=(7, 7), num_filter=32) net = mx.sym.Activation(data=net, act_type="relu") net = mx.sym.Pooling(data=net, name="pool", pool_type="max", kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) net = mx.sym.FullyConnected(data=net, name="fc1", num_hidden=hidden_size) net = mx.sym.Activation(data=net, act_type="relu") net = mx.sym.FullyConnected(data=net, name="fc2", num_hidden=num_classes) net = mx.sym.SoftmaxOutput(data=net, name="softmax", normalization="batch") # Create forward function and add parameters to this model. input_shapes = {"X": (batch_size,) + input_size, "softmax_label": (batch_size,)} self.cnn = Function(net, input_shapes=input_shapes, name="cnn") self.add_params(self.cnn.get_params())
class ConvolutionNet(ModelBase): def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name='X') net = mx.sym.Convolution(data=net, name='conv', kernel=(7, 7), num_filter=32) net = mx.sym.Activation(data=net, act_type='relu') net = mx.sym.Pooling(data=net, name='pool', pool_type='max', kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) net = mx.sym.FullyConnected(data=net, name='fc1', num_hidden=hidden_size) net = mx.sym.Activation(data=net, act_type='relu') net = mx.sym.FullyConnected(data=net, name='fc2', num_hidden=num_classes) net = mx.sym.SoftmaxOutput(data=net, name='output') # Create forward function and add parameters to this model. self.cnn = Function(net, input_shapes={'X': (batch_size, ) + input_size}, name='cnn') self.add_params(self.cnn.get_params()) def forward(self, X, mode): out = self.cnn(X=X, **self.params) return out def loss(self, predict, y): # Add L2 regularization for all the weights. reg_loss = 0.0 for name, weight in self.params.items(): reg_loss += np.sum(weight**2) * 0.5 return layers.softmax_cross_entropy(predict, y) + weight_decay * reg_loss
class ConvolutionNet(ModelBase): def __init__(self): super(ConvolutionNet, self).__init__() # Define symbols that using convolution and max pooling to extract better features # from input image. net = mx.sym.Variable(name='X') net = mx.sym.Convolution(data=net, name='conv', kernel=(7, 7), num_filter=32) net = mx.sym.Activation(data=net, act_type='relu') net = mx.sym.Pooling(data=net, name='pool', pool_type='max', kernel=(2, 2), stride=(2, 2)) net = mx.sym.Flatten(data=net) net = mx.sym.FullyConnected(data=net, name='fc1', num_hidden=hidden_size) net = mx.sym.Activation(data=net, act_type='relu') net = mx.sym.FullyConnected(data=net, name='fc2', num_hidden=num_classes) # Create forward function and add parameters to this model. self.cnn = Function(net, input_shapes={'X': (batch_size, ) + input_size}, name='cnn') self.add_params(self.cnn.get_params()) def forward(self, X, mode): out = self.cnn(X=X, **self.params) return out def loss(self, predict, y): return layers.softmax_loss(predict, y)